T
TB
Hi All:
I am trying to create a variation on the standard datagrid, whereby the
datagrid is only shown after pressing some buttons. This reason for
this is that I would like to use the same datagrid for several tables,
and the idea is that the button events store the the SQL select
statement and the SQL update statement in view state items, which can
the be reused for all the datagrid events (paging and editing).
However I seem to have a problem understanding the life cycle of the
page, which is why I would like to ask for some help here.
The error message I keep getting is:
"Object reference not set to an instance of an object.", always
referring to the line:
dgStaffOptions.DataSource = myDataSet.Tables("mytable"
(contained in a sub called showdatagrid() - 'dgStaffOptions' is the ID
of the datagrid).
Below is the code, which I have simplied for clarity (only the first
button works). The asp:labels and the various reponse.write lines are
elements I have introduced during the bug testing.
HTML side contained in a user control (counter1u.ascx):
<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="counter1u.ascx.vb" Inherits="qmsnet.counter1u"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<h1>Staff Management - Standard options</h1>
<table>
<TBODY>
<tr>
<td vAlign="top">
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300"
border="0">
<TR>
<TD><p>Options</p></TD>
<TD><p>Change</p></TD>
</TR>
<TR>
<TD><p>Positions</p></TD>
<TD align="center"><asp:button id="Btn_Postions" Text="Edit"
runat="server"></asp:button></TD>
</TR>
<TR>
<TD><p>Company</p></TD>
<TD align="center"><asp:button id="Btn_Company" Text="Edit"
runat="server"></asp:button></TD>
</TR>
</TABLE>
<asp:label id="lbltest" Runat="server"></asp:label><br>
<asp:label id="lbltest2" Runat="server"></asp:label>
</td>
<td vAlign="top" width="600">
<asp:datagrid id="dgStaffOptions" runat="server" Runat="server"
OnUpdateCommand="EditDataGrid_Update"
OnCancelCommand="EditDataGrid_Cancel" OnEditCommand="EditDataGrid_Edit"
OnPageIndexChanged="PageChange" AllowPaging="True" PageSize="4"
autogeneratecolumns="False" Width="400px">
<Columns>
<asp:BoundColumn DataField="ID" ReadOnly="True"
HeaderText="#"></asp:BoundColumn>
<asp:BoundColumn DataField="Options"
HeaderText="Options"></asp:BoundColumn>
<asp:EditCommandColumn ItemStyle-CssClass="myListItem"
ButtonType="LinkButton" UpdateText="Update" HeaderText="Edit"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
</Columns>
</asp:datagrid>
</td>
</tr>
</TBODY>
</table>
Code-behind side (counter1u.ascx.vb):
Imports MySql.Data.MySqlClient
Imports qmsnet.test2
Public Class counter1u
Inherits System.Web.UI.UserControl
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Protected WithEvents lbltest As System.Web.UI.WebControls.Label
Protected WithEvents Btn_Postions As
System.Web.UI.WebControls.Button
Protected WithEvents Btn_Company As
System.Web.UI.WebControls.Button
Protected WithEvents dgStaffOptions As
System.Web.UI.WebControls.DataGrid
Protected WithEvents lbltest2 As System.Web.UI.WebControls.Label
'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Dim myConnection As MySqlConnection
Dim myDataAdapter As MySqlDataAdapter
Dim myDataSet As DataSet
Dim strSQLSelect As String
Dim strSQLUpdate As String
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
myConnection = New MySqlConnection("server=mysql.mydomain.com;
user id=root; password=password; database=mybase; pooling=false;")
If Not Page.IsPostBack Then
strSQLSelect = "Select ID, Options from qmsPositions order
by Options"
strSQLUpdate = "Update"
Else
strSQLSelect = ViewState("StrSQLSelect")
strSQLUpdate = ViewState("StrSQLUpdate")
End If
'Response.Write("load:" & strSQLSelect)
LoadDataFromDB()
End Sub
Private Sub Page_Prerender(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.PreRender
Viewstate("strSQLSelect") = strSQLSelect
ViewState("strSQLUpdate") = strSQLUpdate
lbltest.Text = strSQLSelect
lbltest2.Text = strSQLUpdate
End Sub
Sub LoadDataFromDB()
Order by Options"
myDataAdapter = New MySqlDataAdapter(strSQLSelect,
myConnection)
myDataSet = New DataSet
Response.Write("load3:" & strSQLSelect)
myDataAdapter.Fill(myDataSet, "mytable")
End Sub
Private Sub Btn_Postions_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Btn_Postions.Click
strSQLSelect = "Select ID, Options from qmsPositions Order by
Options"
strSQLUpdate = "Update qmsPositions set Options = 'columnvalue'
where ID = idvalue"
Showdatagrid()
End Sub
Private Sub Btn_Company_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Btn_Company.Click
End Sub
Sub PageChange(ByVal sender As Object, ByVal e As
DataGridPageChangedEventArgs)
dgStaffOptions.CurrentPageIndex = e.NewPageIndex
Showdatagrid()
End Sub
Sub Showdatagrid()
dgStaffOptions.DataSource = myDataSet.Tables("mytable")
dgStaffOptions.DataBind()
End Sub
Sub EditDataGrid_Edit(ByVal Sender As Object, ByVal E As
DataGridCommandEventArgs)
dgStaffOptions.EditItemIndex = E.Item.ItemIndex
Showdatagrid()
End Sub
Sub EditDataGrid_Cancel(ByVal Sender As Object, ByVal E As
DataGridCommandEventArgs)
dgStaffOptions.EditItemIndex = -1
Showdatagrid()
End Sub
Sub EditDataGrid_Update(ByVal Sender As Object, ByVal E As
DataGridCommandEventArgs)
Dim IDint As String = E.Item.Cells(0).Text
Dim Options As TextBox = E.Item.Cells(1).Controls(0)
Dim SQLtemp As String
Dim objCommand As MySqlCommand
SQLtemp = Replace(strSQLUpdate, "columnvalue", Options.Text)
SQLtemp = Replace(SQLtemp, "idvalue", IDint)
objCommand = New MySqlCommand(SQLtemp, myConnection)
Try
myConnection.Open()
objCommand.ExecuteNonQuery()
myConnection.Close()
Catch Ex As Exception
Response.Write("<p><strong>An Error Occurred:</strong> " &
Ex.ToString() & "</p>" & vbCrLf)
Finally
myConnection.Close()
End Try
LoadDataFromDB()
dgStaffOptions.EditItemIndex = -1
Showdatagrid()
End Sub
End Class
=====
Thanks a bundle in advance!!
TB
I am trying to create a variation on the standard datagrid, whereby the
datagrid is only shown after pressing some buttons. This reason for
this is that I would like to use the same datagrid for several tables,
and the idea is that the button events store the the SQL select
statement and the SQL update statement in view state items, which can
the be reused for all the datagrid events (paging and editing).
However I seem to have a problem understanding the life cycle of the
page, which is why I would like to ask for some help here.
The error message I keep getting is:
"Object reference not set to an instance of an object.", always
referring to the line:
dgStaffOptions.DataSource = myDataSet.Tables("mytable"
(contained in a sub called showdatagrid() - 'dgStaffOptions' is the ID
of the datagrid).
Below is the code, which I have simplied for clarity (only the first
button works). The asp:labels and the various reponse.write lines are
elements I have introduced during the bug testing.
HTML side contained in a user control (counter1u.ascx):
<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="counter1u.ascx.vb" Inherits="qmsnet.counter1u"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<h1>Staff Management - Standard options</h1>
<table>
<TBODY>
<tr>
<td vAlign="top">
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300"
border="0">
<TR>
<TD><p>Options</p></TD>
<TD><p>Change</p></TD>
</TR>
<TR>
<TD><p>Positions</p></TD>
<TD align="center"><asp:button id="Btn_Postions" Text="Edit"
runat="server"></asp:button></TD>
</TR>
<TR>
<TD><p>Company</p></TD>
<TD align="center"><asp:button id="Btn_Company" Text="Edit"
runat="server"></asp:button></TD>
</TR>
</TABLE>
<asp:label id="lbltest" Runat="server"></asp:label><br>
<asp:label id="lbltest2" Runat="server"></asp:label>
</td>
<td vAlign="top" width="600">
<asp:datagrid id="dgStaffOptions" runat="server" Runat="server"
OnUpdateCommand="EditDataGrid_Update"
OnCancelCommand="EditDataGrid_Cancel" OnEditCommand="EditDataGrid_Edit"
OnPageIndexChanged="PageChange" AllowPaging="True" PageSize="4"
autogeneratecolumns="False" Width="400px">
<Columns>
<asp:BoundColumn DataField="ID" ReadOnly="True"
HeaderText="#"></asp:BoundColumn>
<asp:BoundColumn DataField="Options"
HeaderText="Options"></asp:BoundColumn>
<asp:EditCommandColumn ItemStyle-CssClass="myListItem"
ButtonType="LinkButton" UpdateText="Update" HeaderText="Edit"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
</Columns>
</asp:datagrid>
</td>
</tr>
</TBODY>
</table>
Code-behind side (counter1u.ascx.vb):
Imports MySql.Data.MySqlClient
Imports qmsnet.test2
Public Class counter1u
Inherits System.Web.UI.UserControl
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Protected WithEvents lbltest As System.Web.UI.WebControls.Label
Protected WithEvents Btn_Postions As
System.Web.UI.WebControls.Button
Protected WithEvents Btn_Company As
System.Web.UI.WebControls.Button
Protected WithEvents dgStaffOptions As
System.Web.UI.WebControls.DataGrid
Protected WithEvents lbltest2 As System.Web.UI.WebControls.Label
'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Dim myConnection As MySqlConnection
Dim myDataAdapter As MySqlDataAdapter
Dim myDataSet As DataSet
Dim strSQLSelect As String
Dim strSQLUpdate As String
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
myConnection = New MySqlConnection("server=mysql.mydomain.com;
user id=root; password=password; database=mybase; pooling=false;")
If Not Page.IsPostBack Then
strSQLSelect = "Select ID, Options from qmsPositions order
by Options"
strSQLUpdate = "Update"
Else
strSQLSelect = ViewState("StrSQLSelect")
strSQLUpdate = ViewState("StrSQLUpdate")
End If
'Response.Write("load:" & strSQLSelect)
LoadDataFromDB()
End Sub
Private Sub Page_Prerender(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.PreRender
Viewstate("strSQLSelect") = strSQLSelect
ViewState("strSQLUpdate") = strSQLUpdate
lbltest.Text = strSQLSelect
lbltest2.Text = strSQLUpdate
End Sub
Sub LoadDataFromDB()
Order by Options"
myDataAdapter = New MySqlDataAdapter(strSQLSelect,
myConnection)
myDataSet = New DataSet
Response.Write("load3:" & strSQLSelect)
myDataAdapter.Fill(myDataSet, "mytable")
End Sub
Private Sub Btn_Postions_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Btn_Postions.Click
strSQLSelect = "Select ID, Options from qmsPositions Order by
Options"
strSQLUpdate = "Update qmsPositions set Options = 'columnvalue'
where ID = idvalue"
Showdatagrid()
End Sub
Private Sub Btn_Company_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Btn_Company.Click
End Sub
Sub PageChange(ByVal sender As Object, ByVal e As
DataGridPageChangedEventArgs)
dgStaffOptions.CurrentPageIndex = e.NewPageIndex
Showdatagrid()
End Sub
Sub Showdatagrid()
dgStaffOptions.DataSource = myDataSet.Tables("mytable")
dgStaffOptions.DataBind()
End Sub
Sub EditDataGrid_Edit(ByVal Sender As Object, ByVal E As
DataGridCommandEventArgs)
dgStaffOptions.EditItemIndex = E.Item.ItemIndex
Showdatagrid()
End Sub
Sub EditDataGrid_Cancel(ByVal Sender As Object, ByVal E As
DataGridCommandEventArgs)
dgStaffOptions.EditItemIndex = -1
Showdatagrid()
End Sub
Sub EditDataGrid_Update(ByVal Sender As Object, ByVal E As
DataGridCommandEventArgs)
Dim IDint As String = E.Item.Cells(0).Text
Dim Options As TextBox = E.Item.Cells(1).Controls(0)
Dim SQLtemp As String
Dim objCommand As MySqlCommand
SQLtemp = Replace(strSQLUpdate, "columnvalue", Options.Text)
SQLtemp = Replace(SQLtemp, "idvalue", IDint)
objCommand = New MySqlCommand(SQLtemp, myConnection)
Try
myConnection.Open()
objCommand.ExecuteNonQuery()
myConnection.Close()
Catch Ex As Exception
Response.Write("<p><strong>An Error Occurred:</strong> " &
Ex.ToString() & "</p>" & vbCrLf)
Finally
myConnection.Close()
End Try
LoadDataFromDB()
dgStaffOptions.EditItemIndex = -1
Showdatagrid()
End Sub
End Class
=====
Thanks a bundle in advance!!
TB