INSERT INTO with InsertParameters: Must declare the variable XXX

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();
}
 
F

Fernando Rodriguez

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top