DropDownList web control in DataGrid | | |
Hi folks,
I am getting an error "Object reference not set to an instance of an object".
It seems I have everything in place but something is obviously in err. If
you could take a quick peak at my code segments and provide any insight in a
reply, I would be eternally grateful. My table name is "rfi" and the field
with a dropdownlist control is called "contract". I have imported the
System.Data and System.Data.OleDb libraries. Currently I am running the page
with a local web server. If I run the page without the reference to the
contract field with its associated functions, it works fine. A question
field is shown only as an example field and is not the problem. All code
referencing the contract field may be causing my problem.
' GLOBAL VARIABLE
Dim ddlContractDataSet as DataSet = New DataSet ( )
' UPDATE ROW CODE SNIPPET
Sub DataGrid1_UpdateRow (sender As Object, e As DataGridCommandEventArgs)
If Not Page.IsValid Then Exit Sub
' GET INFO FROM FIELDS
Dim questionTextBox as TextBox = e.Item.Cells (1).FindControl
("txtQuestion") ' THIS LINE WORKS FINE AND IS SHOWN ONLY AS AN EXAMPLE
Dim ddlContracts as DropDownList = e.Item.Cells (2).FindControl
("ddlContract")
...... ' OTHER FIELDS
' PROVIDE A SQL UPDATE STATEMENT
' PROVIDE A CONNECTION STRING AND A CONNECTION
' PROVIDE A dbCOMMAND with COMMANDTEXT=SQL STATEMENT AND A
dbCONNECTION=CONNECTION
' DECLARE A PARAMETER
Dim contractParam as IDataParameter = New OleDbParameter
("@contractParam", OleDbType.varchar, 50)
contractParam.Value = ddlContracts.SelectedIndex
dbCommand.Parameters.Add (contractParam)
End Sub
' PROVIDE FUNCTION TO POPULATE DROPDOWNLIST FROM DB
Function GetContractChoice( ) As DataSet
Dim strConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole
DB Services=-4; Data Source=C:\FDMgmt\webfiles\database\fdmdb.mdb"'
Dim dbConnection As IDbConnection = New OleDbConnection(connectionString)
Dim queryString As String = "SELECT [rfi].[contract] FROM [rfi]"
Dim dbCommand As IDbCommand = New OleDbCommand
Dim dbConnection As IDbConnection = New OleDbConnection(strConnString)
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection
Dim dataAdapter As IDbDataAdapter = New OleDbDataAdapter
dataAdapter.SelectCommand = dbCommand
dataAdapter.Fill(ddlContractDataSet)
Return ddlContractDataSet
End Function
' PROVIDE FUNCTION TO GET THE SELECTED ITEM
Function GetContractSelectedIndex(contract_id as String) as Integer
Dim iLoop as Integer
Dim dt as DataTable = ddlContractDataSet.Tables("rfi")
For iLoop = 0 to dt.Rows.Count - 1
if contract_id = dt.Rows(iLoop)("ID").ToString() then
Return iLoop
end if
Next iLoop
End Function
' PROVIDE DATAGRID INFO IN THE HTML
<asp:DataGrid id="DataGrid1" runat="server"
OnItemDataBound="DataGrid1_RowDataBound"
OnDeleteCommand="DataGrid1_DeleteRow" DataKeyField="ID"
OnCancelCommand="DataGrid1_CancelRow" OnUpdateCommand="DataGrid1_UpdateRow"
OnEditCommand="DataGrid1_EditRow" OnSortCommand="DataGrid1_Sorting"
AllowSorting="True" OnPageIndexChanged="DataGrid1_Paging" AllowPaging="True"
PageSize="2" Width="75%" AutoGenerateColumns="False"
AlternatingItemStyle-BackColor="#dddddd" CellPadding="5" Font-Size="9pt"
Font-Name="Verdana">
<HeaderStyle backcolor="Navy" forecolor="White" font-size="13pt"
font-bold="True" horizontalalign="Center" />
<PagerStyle backcolor="Navy" forecolor="White" font-size="8pt"
font-bold="True" horizontalalign="Right" nextpagetext="Next >"
prevpagetext="< Prev" />
<Columns>
<asp:EditCommandColumn EditText="Edit" UpdateText="Update"
CancelText="Cancel" ButtonType="LinkButton" />
<asp:ButtonColumn Text="Delete" ButtonType="LinkButton"
CommandName="Delete" />
' FIRST COLUMN (NOT THE PROBLEM, SHOWN AS AN EXAMPLE ONLY)
<asp:TemplateColumn HeaderText="Question" SortExpression="question">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "question") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" id="txtQuestion"
Columns="50" MaxLength="2000" Rows="10" Font-Name="Verdana" Font-Size="9pt"
TextMode="MultiLine" Text='<%# DataBinder.Eval(Container.DataItem,
"question") %>' />
<asp:RequiredFieldValidator runat="server"
ControlToValidate="txtQuestion" Display="Dynamic" ErrorMessage="<br />You
must provide a Question." />
</EditItemTemplate>
</asp:TemplateColumn>
' PROVIDE CONTRACT COLUMN INFO (THIS IS CODE THAT MAY BE IN ERR)
<asp:TemplateColumn HeaderText="Contract"
SortExpression="contract">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "contract") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" id="ddlContract"
DataSource="<%# GetContractChoice( ) %>" DataTextField="contract"
DataValueField="ID" SelectedIndex='<%#
GetContractSelectedIndex(Container.DataItem("ID")) %>' Columns="20"
MaxLength="50" Font-Name="Verdana" Font-Size="9pt" Text='<%#
DataBinder.Eval(Container.DataItem, "contract") %>' />
<asp:RequiredFieldValidator runat="server"
ControlToValidate="ddlContract" Display="Dynamic" ErrorMessage="<br />You
must provide a contract." />
</EditItemTemplate>
</asp:TemplateColumn>
..... ' MORE COLUMNS
</Columns>
</asp:DataGrid> | | | | re: DropDownList web control in DataGrid
What line in the code is causing the error?
"glenn" wrote:
[color=blue]
> Hi folks,
>
> I am getting an error "Object reference not set to an instance of an object".
>
> It seems I have everything in place but something is obviously in err. If
> you could take a quick peak at my code segments and provide any insight in a
> reply, I would be eternally grateful. My table name is "rfi" and the field
> with a dropdownlist control is called "contract". I have imported the
> System.Data and System.Data.OleDb libraries. Currently I am running the page
> with a local web server. If I run the page without the reference to the
> contract field with its associated functions, it works fine. A question
> field is shown only as an example field and is not the problem. All code
> referencing the contract field may be causing my problem.
>
> ' GLOBAL VARIABLE
> Dim ddlContractDataSet as DataSet = New DataSet ( )
>
> ' UPDATE ROW CODE SNIPPET
> Sub DataGrid1_UpdateRow (sender As Object, e As DataGridCommandEventArgs)
> If Not Page.IsValid Then Exit Sub
>
> ' GET INFO FROM FIELDS
> Dim questionTextBox as TextBox = e.Item.Cells (1).FindControl
> ("txtQuestion") ' THIS LINE WORKS FINE AND IS SHOWN ONLY AS AN EXAMPLE
> Dim ddlContracts as DropDownList = e.Item.Cells (2).FindControl
> ("ddlContract")
> ...... ' OTHER FIELDS
>
> ' PROVIDE A SQL UPDATE STATEMENT
> ' PROVIDE A CONNECTION STRING AND A CONNECTION
> ' PROVIDE A dbCOMMAND with COMMANDTEXT=SQL STATEMENT AND A
> dbCONNECTION=CONNECTION
> ' DECLARE A PARAMETER
> Dim contractParam as IDataParameter = New OleDbParameter
> ("@contractParam", OleDbType.varchar, 50)
> contractParam.Value = ddlContracts.SelectedIndex
> dbCommand.Parameters.Add (contractParam)
> End Sub
>
> ' PROVIDE FUNCTION TO POPULATE DROPDOWNLIST FROM DB
> Function GetContractChoice( ) As DataSet
> Dim strConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole
> DB Services=-4; Data Source=C:\FDMgmt\webfiles\database\fdmdb.mdb"'
> Dim dbConnection As IDbConnection = New OleDbConnection(connectionString)
> Dim queryString As String = "SELECT [rfi].[contract] FROM [rfi]"
>
> Dim dbCommand As IDbCommand = New OleDbCommand
> Dim dbConnection As IDbConnection = New OleDbConnection(strConnString)
>
> dbCommand.CommandText = queryString
> dbCommand.Connection = dbConnection
>
> Dim dataAdapter As IDbDataAdapter = New OleDbDataAdapter
> dataAdapter.SelectCommand = dbCommand
> dataAdapter.Fill(ddlContractDataSet)
>
> Return ddlContractDataSet
> End Function
>
> ' PROVIDE FUNCTION TO GET THE SELECTED ITEM
> Function GetContractSelectedIndex(contract_id as String) as Integer
> Dim iLoop as Integer
> Dim dt as DataTable = ddlContractDataSet.Tables("rfi")
>
> For iLoop = 0 to dt.Rows.Count - 1
> if contract_id = dt.Rows(iLoop)("ID").ToString() then
> Return iLoop
> end if
> Next iLoop
> End Function
>
> ' PROVIDE DATAGRID INFO IN THE HTML
> <asp:DataGrid id="DataGrid1" runat="server"
> OnItemDataBound="DataGrid1_RowDataBound"
> OnDeleteCommand="DataGrid1_DeleteRow" DataKeyField="ID"
> OnCancelCommand="DataGrid1_CancelRow" OnUpdateCommand="DataGrid1_UpdateRow"
> OnEditCommand="DataGrid1_EditRow" OnSortCommand="DataGrid1_Sorting"
> AllowSorting="True" OnPageIndexChanged="DataGrid1_Paging" AllowPaging="True"
> PageSize="2" Width="75%" AutoGenerateColumns="False"
> AlternatingItemStyle-BackColor="#dddddd" CellPadding="5" Font-Size="9pt"
> Font-Name="Verdana">
> <HeaderStyle backcolor="Navy" forecolor="White" font-size="13pt"
> font-bold="True" horizontalalign="Center" />
> <PagerStyle backcolor="Navy" forecolor="White" font-size="8pt"
> font-bold="True" horizontalalign="Right" nextpagetext="Next >"
> prevpagetext="< Prev" />
> <Columns>
> <asp:EditCommandColumn EditText="Edit" UpdateText="Update"
> CancelText="Cancel" ButtonType="LinkButton" />
> <asp:ButtonColumn Text="Delete" ButtonType="LinkButton"
> CommandName="Delete" />
>
> ' FIRST COLUMN (NOT THE PROBLEM, SHOWN AS AN EXAMPLE ONLY)
>
> <asp:TemplateColumn HeaderText="Question" SortExpression="question">
> <ItemTemplate>
> <%# DataBinder.Eval(Container.DataItem, "question") %>
> </ItemTemplate>
> <EditItemTemplate>
> <asp:TextBox runat="server" id="txtQuestion"
> Columns="50" MaxLength="2000" Rows="10" Font-Name="Verdana" Font-Size="9pt"
> TextMode="MultiLine" Text='<%# DataBinder.Eval(Container.DataItem,
> "question") %>' />
> <asp:RequiredFieldValidator runat="server"
> ControlToValidate="txtQuestion" Display="Dynamic" ErrorMessage="<br />You
> must provide a Question." />
> </EditItemTemplate>
> </asp:TemplateColumn>
>
> ' PROVIDE CONTRACT COLUMN INFO (THIS IS CODE THAT MAY BE IN ERR)
>
> <asp:TemplateColumn HeaderText="Contract"
> SortExpression="contract">
> <ItemTemplate>
> <%# DataBinder.Eval(Container.DataItem, "contract") %>
> </ItemTemplate>
> <EditItemTemplate>
> <asp:DropDownList runat="server" id="ddlContract"
> DataSource="<%# GetContractChoice( ) %>" DataTextField="contract"
> DataValueField="ID" SelectedIndex='<%#
> GetContractSelectedIndex(Container.DataItem("ID")) %>' Columns="20"
> MaxLength="50" Font-Name="Verdana" Font-Size="9pt" Text='<%#
> DataBinder.Eval(Container.DataItem, "contract") %>' />
> <asp:RequiredFieldValidator runat="server"
> ControlToValidate="ddlContract" Display="Dynamic" ErrorMessage="<br />You
> must provide a contract." />
> </EditItemTemplate>
> </asp:TemplateColumn>
>
> ..... ' MORE COLUMNS
>
> </Columns>
> </asp:DataGrid>
>
>
>[/color] | | | | re: DropDownList web control in DataGrid
Thanks for your reply.
The problem occurs when I call the GetContractChoice ( ) function and the
GetContractSelectedIndex function from the <asp:DropDownList ...> in the
html. Not sure exactly what line as I need to block out chunks of code to
get the page to run without errors.
My Update statement and Param defintions are as follows. The problem may
occur in these lines.
"UPDATE rfi SET question =@questionParam, contract =@contractParam, ***ALOT
OF OTHER FIELDS*** WHERE id =@IDParam"
' CONTRACT PARAM IS DEFINED AS FOLLOWS
Dim contractParam as IDataParameter = New
OleDbParameter("@contractParam", OleDbType.varchar, 50)
contractParam.Value = ddlContracts.SelectedIndex
dbCommand.Parameters.Add(contractParam)
"clickon" wrote:
[color=blue]
> What line in the code is causing the error?
>
> "glenn" wrote:
>[color=green]
> > Hi folks,
> >
> > I am getting an error "Object reference not set to an instance of an object".
> >
> > It seems I have everything in place but something is obviously in err. If
> > you could take a quick peak at my code segments and provide any insight in a
> > reply, I would be eternally grateful. My table name is "rfi" and the field
> > with a dropdownlist control is called "contract". I have imported the
> > System.Data and System.Data.OleDb libraries. Currently I am running the page
> > with a local web server. If I run the page without the reference to the
> > contract field with its associated functions, it works fine. A question
> > field is shown only as an example field and is not the problem. All code
> > referencing the contract field may be causing my problem.
> >
> > ' GLOBAL VARIABLE
> > Dim ddlContractDataSet as DataSet = New DataSet ( )
> >
> > ' UPDATE ROW CODE SNIPPET
> > Sub DataGrid1_UpdateRow (sender As Object, e As DataGridCommandEventArgs)
> > If Not Page.IsValid Then Exit Sub
> >
> > ' GET INFO FROM FIELDS
> > Dim questionTextBox as TextBox = e.Item.Cells (1).FindControl
> > ("txtQuestion") ' THIS LINE WORKS FINE AND IS SHOWN ONLY AS AN EXAMPLE
> > Dim ddlContracts as DropDownList = e.Item.Cells (2).FindControl
> > ("ddlContract")
> > ...... ' OTHER FIELDS
> >
> > ' PROVIDE A SQL UPDATE STATEMENT
> > ' PROVIDE A CONNECTION STRING AND A CONNECTION
> > ' PROVIDE A dbCOMMAND with COMMANDTEXT=SQL STATEMENT AND A
> > dbCONNECTION=CONNECTION
> > ' DECLARE A PARAMETER
> > Dim contractParam as IDataParameter = New OleDbParameter
> > ("@contractParam", OleDbType.varchar, 50)
> > contractParam.Value = ddlContracts.SelectedIndex
> > dbCommand.Parameters.Add (contractParam)
> > End Sub
> >
> > ' PROVIDE FUNCTION TO POPULATE DROPDOWNLIST FROM DB
> > Function GetContractChoice( ) As DataSet
> > Dim strConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole
> > DB Services=-4; Data Source=C:\FDMgmt\webfiles\database\fdmdb.mdb"'
> > Dim dbConnection As IDbConnection = New OleDbConnection(connectionString)
> > Dim queryString As String = "SELECT [rfi].[contract] FROM [rfi]"
> >
> > Dim dbCommand As IDbCommand = New OleDbCommand
> > Dim dbConnection As IDbConnection = New OleDbConnection(strConnString)
> >
> > dbCommand.CommandText = queryString
> > dbCommand.Connection = dbConnection
> >
> > Dim dataAdapter As IDbDataAdapter = New OleDbDataAdapter
> > dataAdapter.SelectCommand = dbCommand
> > dataAdapter.Fill(ddlContractDataSet)
> >
> > Return ddlContractDataSet
> > End Function
> >
> > ' PROVIDE FUNCTION TO GET THE SELECTED ITEM
> > Function GetContractSelectedIndex(contract_id as String) as Integer
> > Dim iLoop as Integer
> > Dim dt as DataTable = ddlContractDataSet.Tables("rfi")
> >
> > For iLoop = 0 to dt.Rows.Count - 1
> > if contract_id = dt.Rows(iLoop)("ID").ToString() then
> > Return iLoop
> > end if
> > Next iLoop
> > End Function
> >
> > ' PROVIDE DATAGRID INFO IN THE HTML
> > <asp:DataGrid id="DataGrid1" runat="server"
> > OnItemDataBound="DataGrid1_RowDataBound"
> > OnDeleteCommand="DataGrid1_DeleteRow" DataKeyField="ID"
> > OnCancelCommand="DataGrid1_CancelRow" OnUpdateCommand="DataGrid1_UpdateRow"
> > OnEditCommand="DataGrid1_EditRow" OnSortCommand="DataGrid1_Sorting"
> > AllowSorting="True" OnPageIndexChanged="DataGrid1_Paging" AllowPaging="True"
> > PageSize="2" Width="75%" AutoGenerateColumns="False"
> > AlternatingItemStyle-BackColor="#dddddd" CellPadding="5" Font-Size="9pt"
> > Font-Name="Verdana">
> > <HeaderStyle backcolor="Navy" forecolor="White" font-size="13pt"
> > font-bold="True" horizontalalign="Center" />
> > <PagerStyle backcolor="Navy" forecolor="White" font-size="8pt"
> > font-bold="True" horizontalalign="Right" nextpagetext="Next >"
> > prevpagetext="< Prev" />
> > <Columns>
> > <asp:EditCommandColumn EditText="Edit" UpdateText="Update"
> > CancelText="Cancel" ButtonType="LinkButton" />
> > <asp:ButtonColumn Text="Delete" ButtonType="LinkButton"
> > CommandName="Delete" />
> >
> > ' FIRST COLUMN (NOT THE PROBLEM, SHOWN AS AN EXAMPLE ONLY)
> >
> > <asp:TemplateColumn HeaderText="Question" SortExpression="question">
> > <ItemTemplate>
> > <%# DataBinder.Eval(Container.DataItem, "question") %>
> > </ItemTemplate>
> > <EditItemTemplate>
> > <asp:TextBox runat="server" id="txtQuestion"
> > Columns="50" MaxLength="2000" Rows="10" Font-Name="Verdana" Font-Size="9pt"
> > TextMode="MultiLine" Text='<%# DataBinder.Eval(Container.DataItem,
> > "question") %>' />
> > <asp:RequiredFieldValidator runat="server"
> > ControlToValidate="txtQuestion" Display="Dynamic" ErrorMessage="<br />You
> > must provide a Question." />
> > </EditItemTemplate>
> > </asp:TemplateColumn>
> >
> > ' PROVIDE CONTRACT COLUMN INFO (THIS IS CODE THAT MAY BE IN ERR)
> >
> > <asp:TemplateColumn HeaderText="Contract"
> > SortExpression="contract">
> > <ItemTemplate>
> > <%# DataBinder.Eval(Container.DataItem, "contract") %>
> > </ItemTemplate>
> > <EditItemTemplate>
> > <asp:DropDownList runat="server" id="ddlContract"
> > DataSource="<%# GetContractChoice( ) %>" DataTextField="contract"
> > DataValueField="ID" SelectedIndex='<%#
> > GetContractSelectedIndex(Container.DataItem("ID")) %>' Columns="20"
> > MaxLength="50" Font-Name="Verdana" Font-Size="9pt" Text='<%#
> > DataBinder.Eval(Container.DataItem, "contract") %>' />
> > <asp:RequiredFieldValidator runat="server"
> > ControlToValidate="ddlContract" Display="Dynamic" ErrorMessage="<br />You
> > must provide a contract." />
> > </EditItemTemplate>
> > </asp:TemplateColumn>
> >
> > ..... ' MORE COLUMNS
> >
> > </Columns>
> > </asp:DataGrid>
> >
> >
> >[/color][/color] | | | | re: DropDownList web control in DataGrid
Does the error message not say something like below with the line that is
causing the error highlighted ?
Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.
Source Error:
Line 228: For i As Integer = 1 To 35
Line 229: field = "Q" + CStr(i)
Line 230: score.ID = "txtQ" + CStr(i)
Line 231: If Not
IsDBNull(ds.Tables("mmsSpecRecord").Rows(0)(field) ) Then
Line 232: score.Text =
ds.Tables("mmsSpecRecord").Rows(0)(field)
Source File: c:\inetpub\wwwroot\Demo\displayMMS.aspx.vb Line: 230
"glenn" wrote:
[color=blue]
> Thanks for your reply.
>
> The problem occurs when I call the GetContractChoice ( ) function and the
> GetContractSelectedIndex function from the <asp:DropDownList ...> in the
> html. Not sure exactly what line as I need to block out chunks of code to
> get the page to run without errors.
>
> My Update statement and Param defintions are as follows. The problem may
> occur in these lines.
>
> "UPDATE rfi SET question =@questionParam, contract =@contractParam, ***ALOT
> OF OTHER FIELDS*** WHERE id =@IDParam"
>
> ' CONTRACT PARAM IS DEFINED AS FOLLOWS
> Dim contractParam as IDataParameter = New
> OleDbParameter("@contractParam", OleDbType.varchar, 50)
> contractParam.Value = ddlContracts.SelectedIndex
> dbCommand.Parameters.Add(contractParam)
>
>
>
> "clickon" wrote:
>[color=green]
> > What line in the code is causing the error?
> >
> > "glenn" wrote:
> >[color=darkred]
> > > Hi folks,
> > >
> > > I am getting an error "Object reference not set to an instance of an object".
> > >
> > > It seems I have everything in place but something is obviously in err. If
> > > you could take a quick peak at my code segments and provide any insight in a
> > > reply, I would be eternally grateful. My table name is "rfi" and the field
> > > with a dropdownlist control is called "contract". I have imported the
> > > System.Data and System.Data.OleDb libraries. Currently I am running the page
> > > with a local web server. If I run the page without the reference to the
> > > contract field with its associated functions, it works fine. A question
> > > field is shown only as an example field and is not the problem. All code
> > > referencing the contract field may be causing my problem.
> > >
> > > ' GLOBAL VARIABLE
> > > Dim ddlContractDataSet as DataSet = New DataSet ( )
> > >
> > > ' UPDATE ROW CODE SNIPPET
> > > Sub DataGrid1_UpdateRow (sender As Object, e As DataGridCommandEventArgs)
> > > If Not Page.IsValid Then Exit Sub
> > >
> > > ' GET INFO FROM FIELDS
> > > Dim questionTextBox as TextBox = e.Item.Cells (1).FindControl
> > > ("txtQuestion") ' THIS LINE WORKS FINE AND IS SHOWN ONLY AS AN EXAMPLE
> > > Dim ddlContracts as DropDownList = e.Item.Cells (2).FindControl
> > > ("ddlContract")
> > > ...... ' OTHER FIELDS
> > >
> > > ' PROVIDE A SQL UPDATE STATEMENT
> > > ' PROVIDE A CONNECTION STRING AND A CONNECTION
> > > ' PROVIDE A dbCOMMAND with COMMANDTEXT=SQL STATEMENT AND A
> > > dbCONNECTION=CONNECTION
> > > ' DECLARE A PARAMETER
> > > Dim contractParam as IDataParameter = New OleDbParameter
> > > ("@contractParam", OleDbType.varchar, 50)
> > > contractParam.Value = ddlContracts.SelectedIndex
> > > dbCommand.Parameters.Add (contractParam)
> > > End Sub
> > >
> > > ' PROVIDE FUNCTION TO POPULATE DROPDOWNLIST FROM DB
> > > Function GetContractChoice( ) As DataSet
> > > Dim strConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole
> > > DB Services=-4; Data Source=C:\FDMgmt\webfiles\database\fdmdb.mdb"'
> > > Dim dbConnection As IDbConnection = New OleDbConnection(connectionString)
> > > Dim queryString As String = "SELECT [rfi].[contract] FROM [rfi]"
> > >
> > > Dim dbCommand As IDbCommand = New OleDbCommand
> > > Dim dbConnection As IDbConnection = New OleDbConnection(strConnString)
> > >
> > > dbCommand.CommandText = queryString
> > > dbCommand.Connection = dbConnection
> > >
> > > Dim dataAdapter As IDbDataAdapter = New OleDbDataAdapter
> > > dataAdapter.SelectCommand = dbCommand
> > > dataAdapter.Fill(ddlContractDataSet)
> > >
> > > Return ddlContractDataSet
> > > End Function
> > >
> > > ' PROVIDE FUNCTION TO GET THE SELECTED ITEM
> > > Function GetContractSelectedIndex(contract_id as String) as Integer
> > > Dim iLoop as Integer
> > > Dim dt as DataTable = ddlContractDataSet.Tables("rfi")
> > >
> > > For iLoop = 0 to dt.Rows.Count - 1
> > > if contract_id = dt.Rows(iLoop)("ID").ToString() then
> > > Return iLoop
> > > end if
> > > Next iLoop
> > > End Function
> > >
> > > ' PROVIDE DATAGRID INFO IN THE HTML
> > > <asp:DataGrid id="DataGrid1" runat="server"
> > > OnItemDataBound="DataGrid1_RowDataBound"
> > > OnDeleteCommand="DataGrid1_DeleteRow" DataKeyField="ID"
> > > OnCancelCommand="DataGrid1_CancelRow" OnUpdateCommand="DataGrid1_UpdateRow"
> > > OnEditCommand="DataGrid1_EditRow" OnSortCommand="DataGrid1_Sorting"
> > > AllowSorting="True" OnPageIndexChanged="DataGrid1_Paging" AllowPaging="True"
> > > PageSize="2" Width="75%" AutoGenerateColumns="False"
> > > AlternatingItemStyle-BackColor="#dddddd" CellPadding="5" Font-Size="9pt"
> > > Font-Name="Verdana">
> > > <HeaderStyle backcolor="Navy" forecolor="White" font-size="13pt"
> > > font-bold="True" horizontalalign="Center" />
> > > <PagerStyle backcolor="Navy" forecolor="White" font-size="8pt"
> > > font-bold="True" horizontalalign="Right" nextpagetext="Next >"
> > > prevpagetext="< Prev" />
> > > <Columns>
> > > <asp:EditCommandColumn EditText="Edit" UpdateText="Update"
> > > CancelText="Cancel" ButtonType="LinkButton" />
> > > <asp:ButtonColumn Text="Delete" ButtonType="LinkButton"
> > > CommandName="Delete" />
> > >
> > > ' FIRST COLUMN (NOT THE PROBLEM, SHOWN AS AN EXAMPLE ONLY)
> > >
> > > <asp:TemplateColumn HeaderText="Question" SortExpression="question">
> > > <ItemTemplate>
> > > <%# DataBinder.Eval(Container.DataItem, "question") %>
> > > </ItemTemplate>
> > > <EditItemTemplate>
> > > <asp:TextBox runat="server" id="txtQuestion"
> > > Columns="50" MaxLength="2000" Rows="10" Font-Name="Verdana" Font-Size="9pt"
> > > TextMode="MultiLine" Text='<%# DataBinder.Eval(Container.DataItem,
> > > "question") %>' />
> > > <asp:RequiredFieldValidator runat="server"
> > > ControlToValidate="txtQuestion" Display="Dynamic" ErrorMessage="<br />You
> > > must provide a Question." />
> > > </EditItemTemplate>
> > > </asp:TemplateColumn>
> > >
> > > ' PROVIDE CONTRACT COLUMN INFO (THIS IS CODE THAT MAY BE IN ERR)
> > >
> > > <asp:TemplateColumn HeaderText="Contract"
> > > SortExpression="contract">
> > > <ItemTemplate>
> > > <%# DataBinder.Eval(Container.DataItem, "contract") %>
> > > </ItemTemplate>
> > > <EditItemTemplate>
> > > <asp:DropDownList runat="server" id="ddlContract"
> > > DataSource="<%# GetContractChoice( ) %>" DataTextField="contract"
> > > DataValueField="ID" SelectedIndex='<%#
> > > GetContractSelectedIndex(Container.DataItem("ID")) %>' Columns="20"
> > > MaxLength="50" Font-Name="Verdana" Font-Size="9pt" Text='<%#
> > > DataBinder.Eval(Container.DataItem, "contract") %>' />
> > > <asp:RequiredFieldValidator runat="server"
> > > ControlToValidate="ddlContract" Display="Dynamic" ErrorMessage="<br />You
> > > must provide a contract." />
> > > </EditItemTemplate>
> > > </asp:TemplateColumn>
> > >
> > > ..... ' MORE COLUMNS
> > >
> > > </Columns>
> > > </asp:DataGrid>
> > >
> > >
> > >[/color][/color][/color] | | | | re: DropDownList web control in DataGrid
I get the following in my error message:
[NullReferenceException: Object reference not set to an instance of an
object.]
ASP.rfiAccess_aspx.GetContractSelectedIndex(String contract_id) +45
ASP.rfiAccess_aspx.__DataBind__control18(Object sender, EventArgs e) +141
"clickon" wrote:
[color=blue]
> Does the error message not say something like below with the line that is
> causing the error highlighted ?
>
> Exception Details: System.NullReferenceException: Object reference not set
> to an instance of an object.
>
> Source Error:
>
>
> Line 228: For i As Integer = 1 To 35
> Line 229: field = "Q" + CStr(i)
> Line 230: score.ID = "txtQ" + CStr(i)
> Line 231: If Not
> IsDBNull(ds.Tables("mmsSpecRecord").Rows(0)(field) ) Then
> Line 232: score.Text =
> ds.Tables("mmsSpecRecord").Rows(0)(field)
>
>
> Source File: c:\inetpub\wwwroot\Demo\displayMMS.aspx.vb Line: 230
>
> "glenn" wrote:
>[color=green]
> > Thanks for your reply.
> >
> > The problem occurs when I call the GetContractChoice ( ) function and the
> > GetContractSelectedIndex function from the <asp:DropDownList ...> in the
> > html. Not sure exactly what line as I need to block out chunks of code to
> > get the page to run without errors.
> >
> > My Update statement and Param defintions are as follows. The problem may
> > occur in these lines.
> >
> > "UPDATE rfi SET question =@questionParam, contract =@contractParam, ***ALOT
> > OF OTHER FIELDS*** WHERE id =@IDParam"
> >
> > ' CONTRACT PARAM IS DEFINED AS FOLLOWS
> > Dim contractParam as IDataParameter = New
> > OleDbParameter("@contractParam", OleDbType.varchar, 50)
> > contractParam.Value = ddlContracts.SelectedIndex
> > dbCommand.Parameters.Add(contractParam)
> >
> >
> >
> > "clickon" wrote:
> >[color=darkred]
> > > What line in the code is causing the error?
> > >
> > > "glenn" wrote:
> > >
> > > > Hi folks,
> > > >
> > > > I am getting an error "Object reference not set to an instance of an object".
> > > >
> > > > It seems I have everything in place but something is obviously in err. If
> > > > you could take a quick peak at my code segments and provide any insight in a
> > > > reply, I would be eternally grateful. My table name is "rfi" and the field
> > > > with a dropdownlist control is called "contract". I have imported the
> > > > System.Data and System.Data.OleDb libraries. Currently I am running the page
> > > > with a local web server. If I run the page without the reference to the
> > > > contract field with its associated functions, it works fine. A question
> > > > field is shown only as an example field and is not the problem. All code
> > > > referencing the contract field may be causing my problem.
> > > >
> > > > ' GLOBAL VARIABLE
> > > > Dim ddlContractDataSet as DataSet = New DataSet ( )
> > > >
> > > > ' UPDATE ROW CODE SNIPPET
> > > > Sub DataGrid1_UpdateRow (sender As Object, e As DataGridCommandEventArgs)
> > > > If Not Page.IsValid Then Exit Sub
> > > >
> > > > ' GET INFO FROM FIELDS
> > > > Dim questionTextBox as TextBox = e.Item.Cells (1).FindControl
> > > > ("txtQuestion") ' THIS LINE WORKS FINE AND IS SHOWN ONLY AS AN EXAMPLE
> > > > Dim ddlContracts as DropDownList = e.Item.Cells (2).FindControl
> > > > ("ddlContract")
> > > > ...... ' OTHER FIELDS
> > > >
> > > > ' PROVIDE A SQL UPDATE STATEMENT
> > > > ' PROVIDE A CONNECTION STRING AND A CONNECTION
> > > > ' PROVIDE A dbCOMMAND with COMMANDTEXT=SQL STATEMENT AND A
> > > > dbCONNECTION=CONNECTION
> > > > ' DECLARE A PARAMETER
> > > > Dim contractParam as IDataParameter = New OleDbParameter
> > > > ("@contractParam", OleDbType.varchar, 50)
> > > > contractParam.Value = ddlContracts.SelectedIndex
> > > > dbCommand.Parameters.Add (contractParam)
> > > > End Sub
> > > >
> > > > ' PROVIDE FUNCTION TO POPULATE DROPDOWNLIST FROM DB
> > > > Function GetContractChoice( ) As DataSet
> > > > Dim strConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole
> > > > DB Services=-4; Data Source=C:\FDMgmt\webfiles\database\fdmdb.mdb"'
> > > > Dim dbConnection As IDbConnection = New OleDbConnection(connectionString)
> > > > Dim queryString As String = "SELECT [rfi].[contract] FROM [rfi]"
> > > >
> > > > Dim dbCommand As IDbCommand = New OleDbCommand
> > > > Dim dbConnection As IDbConnection = New OleDbConnection(strConnString)
> > > >
> > > > dbCommand.CommandText = queryString
> > > > dbCommand.Connection = dbConnection
> > > >
> > > > Dim dataAdapter As IDbDataAdapter = New OleDbDataAdapter
> > > > dataAdapter.SelectCommand = dbCommand
> > > > dataAdapter.Fill(ddlContractDataSet)
> > > >
> > > > Return ddlContractDataSet
> > > > End Function
> > > >
> > > > ' PROVIDE FUNCTION TO GET THE SELECTED ITEM
> > > > Function GetContractSelectedIndex(contract_id as String) as Integer
> > > > Dim iLoop as Integer
> > > > Dim dt as DataTable = ddlContractDataSet.Tables("rfi")
> > > >
> > > > For iLoop = 0 to dt.Rows.Count - 1
> > > > if contract_id = dt.Rows(iLoop)("ID").ToString() then
> > > > Return iLoop
> > > > end if
> > > > Next iLoop
> > > > End Function
> > > >
> > > > ' PROVIDE DATAGRID INFO IN THE HTML
> > > > <asp:DataGrid id="DataGrid1" runat="server"
> > > > OnItemDataBound="DataGrid1_RowDataBound"
> > > > OnDeleteCommand="DataGrid1_DeleteRow" DataKeyField="ID"
> > > > OnCancelCommand="DataGrid1_CancelRow" OnUpdateCommand="DataGrid1_UpdateRow"
> > > > OnEditCommand="DataGrid1_EditRow" OnSortCommand="DataGrid1_Sorting"
> > > > AllowSorting="True" OnPageIndexChanged="DataGrid1_Paging" AllowPaging="True"
> > > > PageSize="2" Width="75%" AutoGenerateColumns="False"
> > > > AlternatingItemStyle-BackColor="#dddddd" CellPadding="5" Font-Size="9pt"
> > > > Font-Name="Verdana">
> > > > <HeaderStyle backcolor="Navy" forecolor="White" font-size="13pt"
> > > > font-bold="True" horizontalalign="Center" />
> > > > <PagerStyle backcolor="Navy" forecolor="White" font-size="8pt"
> > > > font-bold="True" horizontalalign="Right" nextpagetext="Next >"
> > > > prevpagetext="< Prev" />
> > > > <Columns>
> > > > <asp:EditCommandColumn EditText="Edit" UpdateText="Update"
> > > > CancelText="Cancel" ButtonType="LinkButton" />
> > > > <asp:ButtonColumn Text="Delete" ButtonType="LinkButton"
> > > > CommandName="Delete" />
> > > >
> > > > ' FIRST COLUMN (NOT THE PROBLEM, SHOWN AS AN EXAMPLE ONLY)
> > > >
> > > > <asp:TemplateColumn HeaderText="Question" SortExpression="question">
> > > > <ItemTemplate>
> > > > <%# DataBinder.Eval(Container.DataItem, "question") %>
> > > > </ItemTemplate>
> > > > <EditItemTemplate>
> > > > <asp:TextBox runat="server" id="txtQuestion"
> > > > Columns="50" MaxLength="2000" Rows="10" Font-Name="Verdana" Font-Size="9pt"
> > > > TextMode="MultiLine" Text='<%# DataBinder.Eval(Container.DataItem,
> > > > "question") %>' />
> > > > <asp:RequiredFieldValidator runat="server"
> > > > ControlToValidate="txtQuestion" Display="Dynamic" ErrorMessage="<br />You
> > > > must provide a Question." />
> > > > </EditItemTemplate>
> > > > </asp:TemplateColumn>
> > > >
> > > > ' PROVIDE CONTRACT COLUMN INFO (THIS IS CODE THAT MAY BE IN ERR)
> > > >
> > > > <asp:TemplateColumn HeaderText="Contract"
> > > > SortExpression="contract">
> > > > <ItemTemplate>
> > > > <%# DataBinder.Eval(Container.DataItem, "contract") %>
> > > > </ItemTemplate>
> > > > <EditItemTemplate>
> > > > <asp:DropDownList runat="server" id="ddlContract"
> > > > DataSource="<%# GetContractChoice( ) %>" DataTextField="contract"
> > > > DataValueField="ID" SelectedIndex='<%#
> > > > GetContractSelectedIndex(Container.DataItem("ID")) %>' Columns="20"
> > > > MaxLength="50" Font-Name="Verdana" Font-Size="9pt" Text='<%#
> > > > DataBinder.Eval(Container.DataItem, "contract") %>' />
> > > > <asp:RequiredFieldValidator runat="server"
> > > > ControlToValidate="ddlContract" Display="Dynamic" ErrorMessage="<br />You
> > > > must provide a contract." />
> > > > </EditItemTemplate>
> > > > </asp:TemplateColumn>
> > > >
> > > > ..... ' MORE COLUMNS
> > > >
> > > > </Columns>
> > > > </asp:DataGrid>
> > > >
> > > >
> > > >[/color][/color][/color] |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|