มีตารางสองตาราง คือ ตาราง department และ ตาราง formeverymonth และใช้ gridview แสดงข้อมูลของสองตารางนี้ ปัญหาอยู่ที่การติดต่อฐานข้อมูลสองครั้งมีปัญหาค่ะ รบกวนช่วยแนะนำหน่อยค่ะ
เกิด error แบบนี้ค่ะ Not allowed to change the 'ConnectionString' property. The connection's current state is open
โค้ดค่ะ
Code (VB.NET)
<%@ Page Title="" Language="VB" MasterPageFile="masterform.master" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
Dim objConn As New OleDbConnection
Dim objCmd As New OleDbCommand
Dim strSQL, strConnString As String
Dim jan1 As Integer
Dim dtAdapter As New OleDbDataAdapter
Dim ds As New DataSet
Sub Page_Load(sender As Object, e As EventArgs)
Dim strConnString As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Server.MapPath("database/form.mdb") & ";"
objConn = New OleDbConnection(strConnString)
objConn.Open()
If Not Page.IsPostBack() Then
GridViewBindData()
'GridViewBindData1()
End If
End Sub
Sub GridViewBindData()
Dim objConn As New OleDbConnection
Dim objCmd As New OleDbCommand
Dim dtAdapter As New OleDbDataAdapter
Dim ds As New DataSet
Dim strConnString, strSQL As String
Dim year_se As String = Me.se_years.Text
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("database/form.mdb") & ";Jet OLEDB:Database Password=;"
strSQL = "SELECT * from department "
objConn.ConnectionString = strConnString
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
dtAdapter.SelectCommand = objCmd
dtAdapter.Fill(ds)
'*** BindData to GridView ***'
GridView.DataSource = ds
GridView.DataBind()
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
End Sub
Sub Page_UnLoad()
objConn.Close()
objConn = Nothing
End Sub
Private Sub myGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
Dim IdDepartment As Label = CType(e.Row.FindControl("IdDepartment"), Label)
If Not IsNothing(IdDepartment) Then
IdDepartment.Text = e.Row.DataItem("IdDepartment")
'namedepartment.NavigateUrl = "showdetailnews.aspx?IdNews=" & e.Row.DataItem("")
End If
Dim namedepartment As Label = CType(e.Row.FindControl("namedepartment"), Label)
If Not IsNothing(namedepartment) Then
namedepartment.Text = e.Row.DataItem("NameDepartment")
End If
Dim Jan As HyperLink = CType(e.Row.FindControl("Jan"), HyperLink)
If Not IsNothing(Jan) Then
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("database/form.mdb") & ";Jet OLEDB:Database Password=;"
strSQL = "SELECT * FROM formeverymonth WHERE IdDepartment =" & e.Row.DataItem("IdDepartment") & " And ryear = '" & Me.se_years.Text & "' And rmonth ='1'"
objConn.ConnectionString = strConnString
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
dtAdapter.SelectCommand = objCmd
dtAdapter.Fill(ds)
'*** BindData to GridView ***'
GridView.DataSource = ds
GridView.DataBind()
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
Jan.Text = e.Row.DataItem("Id")
End If
End Sub
Sub se_years_SelectedIndexChang(ByVal sender As Object, ByVal e As System.EventArgs)
'GridViewBindData1()
End Sub
Sub ShowPageCommand(ByVal s As Object, ByVal e As GridViewPageEventArgs)
GridView.PageIndex = e.NewPageIndex
GridViewBindData()
End Sub
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<form id="form1" runat="server" >
<p align="center"><font size="3">เลือกปี : </font><asp:DropDownList ID="se_years" runat="server" AutoPostBack="True"
DataSourceID="AccessDataSource1" DataTextField="ryear"
DataValueField="ryear"
onselectedindexchanged="se_years_SelectedIndexChang"></asp:DropDownList>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/form/database/form.mdb"
SelectCommand="SELECT [ryear] FROM [formeverymonth] GROUP BY [ryear] ORDER BY [ryear] ASC"></asp:AccessDataSource>
</p>
<table border ="1" align="center" runat ="server" >
<tr>
<td align ="center" >
<asp:GridView id="GridView" runat="server" AutoGenerateColumns="False"
onRowDataBound="myGridView_RowDataBound" DataKeyNames="IdDepartment"
PageSize="15" OnPageIndexChanging="ShowPageCommand" AllowPaging="True">
<Columns>
<asp:TemplateField Visible = "false">
<ItemTemplate>
<asp:Label id="IdDepartment" Visible = "False" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ชื่อหน่วยงาน">
<ItemTemplate>
<asp:Label id="namedepartment" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Jan">
<ItemTemplate>
<asp:HyperLink id="Jan" runat="server"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</form>
</asp:Content>