INSERT INTO with InsertParameters: Must declare the variable XXX

  • Thread starter Thread starter seanmatthewwalsh
  • Start date Start date
S

seanmatthewwalsh

Hi

What's wrong with this code, i keep getting an error "Must declar the
variable @MyTitle"

Thanks

-----HTML CODE-----
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Conn1%>"
InsertCommand="INSERT INTO Links(Title) VALUES (@MyTitle)">
<InsertParameters>
<asp:ControlParameter PropertyName="text" ControlID="txtWebsiteTitle"
Name="@MyTitle" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<asp:TextBox text="XXXXXXXXXXXtitle from default" runat="server"
id="txtWebsiteTitle" />
<asp:Button id="btnLinkExchange" onclick="btnLinkExchange_click"
runat="server" Text="Submit Link" />

-----CODEBEHIND-----
protected void btnLinkExchange_click(object sender,
System.EventArgs e)
{
SqlDataSource1.Insert();
}
 
The problem is the @ sign before the param name on your ControlParameter
tag.

basically your command is generating the following SQL code:

DECLARE @@MyTitle as nvarchar(max)
SET @@MyTitle = '<param value>'
INSERT INTO Links(Title) VALUES (@MyTitle)

if you run that code you'll get that error because you declared @@MyTitle
but instead you're trying to use @MyTitle, so either remove the @ sign from
the ControlParameter (recommended) or add another @ to the INSERT statement.
 
Back
Top