Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Msg: Must declare variable @ID

Status
Not open for further replies.

tjherman

Programmer
Jun 19, 2006
68
US
I’m having trouble with a datalist. The default view is the Item Template which has an Edit button. When I click the Edit button, I run the following code (on the EditCommand of the Datalist):

DataList1.EditItemIndex = e.Item.ItemIndex
DataBind()

It errors out with the message “Must declare variable @ID”.

I’ve used this process on other pages without problem. I’m wondering if it has to do with the DataKeyName?

The list of rows in the datalist are based off the ID field matching a session variable that has been set. However, the DataKeyName for the DataList is a field named “NBID” (which is the auto-incrementing primary key, unique ID for the table).

If this is the problem, how can I get around this? I guess, how could I intercept the databind and pass it the parameter it wants? Or is it something else?

Here’s my markup of the SQLDataSource and the Datalist/Edit Template:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SMARTConnectionString %>"
SelectCommand="SELECT * FROM [tblSalesSupport] WHERE ([ID] = @ID)" >
<SelectParameters>
<asp:SessionParameter Name="ID" SessionField="MyID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

<asp:DataList CssClass="MainFormDisplay" ID="DataList1" runat="server" DataKeyField="NBID" DataSourceID="SqlDataSource1" width="100%">
<HeaderTemplate>….</HeaderTemplate>
<ItemTemplate>….</ItemTemplate>
<EditItemTemplate>
<table border="0" style="width: 100%">
<tr class="MainFormDisplay" valign="top">
<td colspan="8">
<asp:TextBox ID="txtNBID" runat="server" Text='<%# Eval("NBID") %>' Visible="true"></asp:TextBox>
<asp:TextBox ID="txtID" runat="server" Text='<%# Bind("ID") %>' Visible="True"></asp:TextBox></td>
</tr>
<tr class="MainFormDisplay">
<td valign="top" style="width: 100px"><asp:Checkbox ID="chkNBNC" runat="server" Checked='<%# Bind("NBNC") %>' /></td>
<td style="width: 100"><asp:CheckBox ID="chkNBEC" runat="server" Checked='<%# Bind("NBEC") %>' Width="100px" /></td>
<td style="width: 100px"><asp:TextBox ID="txtCompanyName" runat="server" Text='<%# Bind("CompanyName")%>' Width="100px"></asp:TextBox></td>
<td style="width: 100px"><asp:TextBox ID="txtProjectNumber" runat="server" Text='<%# Bind("ProjectNumber") %>' Width="100px"></asp:TextBox></td>
<td style="width: 100px"><asp:TextBox ID="txtDescription" runat="server" Text='<%# Bind("Description") %>' Width="100px"></asp:TextBox></td>
<td style="width: 100px"><asp:TextBox ID="txtEstimate" runat="server" Text='<%# Bind("Estimate","{0:N2}") %>' Width="100px"></asp:TextBox></td>
<td style="width: 55px"><asp:CheckBox ID="ckDeleteFlag" runat="server" /></td>
<td style="width: 100px"><asp:Button ID="ItemSaveButton" runat="server" CommandName="Update" Text="Save" />
<asp:Button ID="ItemCancelButton" runat="server" CommandName="Cancel" Text="Cancel" /></td>
</tr>
</table>
&nbsp;
</EditItemTemplate>
</asp:DataList>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top