none
Must declare the scalar variable "@Fruit". RRS feed

  • Question

  • I know there are some other threads similar to this, but none have assisted me in my problem.

    I'm getting the above error When I try to insert a record to my database.

    The Database Table name is "updateme"

    I'm not using any datasets, or anything like this, which I'm hoping isn't the problem.

     

    Code Snippet

    DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

    Protected Sub btnAddProduct_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddProduct.Click

    'Add the new product!

    fruittable.insert()

    End Sub

    </< FONT>script>

     

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head id="Head1" runat="server">

    <title>Fruit Update</< FONT>title>

    </< FONT>head>

    <body>

    <form id="form1" runat="server">

    <div>

    <asp:SqlDataSource ID="FruitTable" runat="server"

    ConnectionString=""

    SelectCommand="SELECT * FROM [updateme]"

    InsertCommand="INSERT INTO updateme(Fruit, Count)

    VALUES (@Fruit, @Count)"

    UpdateCommand="UPDATE updateme SET Fruit =, Count =" >

    </< FONT>asp:SqlDataSource>

    <br />

    <br />

    <br />

    <br />

    Fruit Box:

    <asp:TextBox ID="fruitbox" runat="server"></< FONT>asp:TextBox>

    <br />

    Count Box:

    <asp:TextBox ID="countbox" runat="server"></< FONT>asp:TextBox>

    <br />

    <br />

    <asp:Button ID="btnAddProduct" runat="server" onclick="btnAddProduct_Click"

    Text="Button" />

    <br />

    <br />

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

    DataSourceID="FruitTable">

    <Columns>

    <asp:BoundField DataField="Fruit" HeaderText="Fruit" SortExpression="Fruit" />

    <asp:BoundField DataField="Count" HeaderText="Count" SortExpression="Count" />

    </< FONT>Columns>

    </< FONT>asp:GridView>

    <br />

    </< FONT>div>

    </< FONT>form>

    </< FONT>body>

    </< FONT>html>

     

     

    I can pull the data from the Table and display it. When I run the Insert Query before insertion, I can update the table. As soon as it's live, I get an error.
    Monday, April 7, 2008 9:39 PM

Answers

  • I believe Count is a reserved word and it might cause problem. Try to add square brackets around column nmaes inside of your SQL statements, like

     

    InsertCommand="INSERT INTO [updateme] ([Fruit], [Count])  VALUES (@Fruit, @Count)"

     

    You also need to supply parameters to your UPDATE SQL statement, like

     

    UpdateCommand="UPDATE [updateme] SET [Fruit] =@Fruit, [Count] =@Count"

     

    You also need to specify binding of columns inside of FruitTable to the parameters, so DataAdapter know how to get data from DataTable when it executes any action (INSERT/UPDATE/DELETE) queries

    Tuesday, April 8, 2008 10:21 AM
    Moderator

All replies

  • I believe Count is a reserved word and it might cause problem. Try to add square brackets around column nmaes inside of your SQL statements, like

     

    InsertCommand="INSERT INTO [updateme] ([Fruit], [Count])  VALUES (@Fruit, @Count)"

     

    You also need to supply parameters to your UPDATE SQL statement, like

     

    UpdateCommand="UPDATE [updateme] SET [Fruit] =@Fruit, [Count] =@Count"

     

    You also need to specify binding of columns inside of FruitTable to the parameters, so DataAdapter know how to get data from DataTable when it executes any action (INSERT/UPDATE/DELETE) queries

    Tuesday, April 8, 2008 10:21 AM
    Moderator
  • It might help to identify the type of database you are working with.

     

    Tuesday, April 8, 2008 12:40 PM
  •  

    Microsoft Sql Server 2005 Database

    I tried the brackets around everything, as the example suggested, and still no luck.

    And even if "Count" was a reserved word, wouldn't it bomb out on "Count" and not "Fruit"?

    Here's My Web.Config:

     

     

    Code Snippet

    <?xml version="1.0"?>

    <!--

    Note: As an alternative to hand editing this file you can use the

    web admin tool to configure settings for your application. Use

    the Website->Asp.Net Configuration option in Visual Studio.

    A full list of settings and comments can be found in

    machine.config.comments usually located in

    \Windows\Microsoft.Net\Framework\v2.x\Config

    -->

    <configuration>

    <appSettings/>

    <connectionStrings>

    <remove name="LocalSqlServer" />

    <add name="mirondbConnectionString" connectionString="Data Source=IRONHORSE;Initial Catalog=mirondb;user id=sa;password=xxxxxx;Integrated Security=False" providerName="System.Data.SqlClient"/>

    </connectionStrings>

    <system.web>

    <!--

    Set compilation debug="true" to insert debugging

    symbols into the compiled page. Because this

    affects performance, set this value to true only

    during development.

    -->

    <compilation debug="true">

    </compilation>

    <!--

    The <authentication> section enables configuration

    of the security authentication mode used by

    ASP.NET to identify an incoming user.

    -->

    <authentication mode="Windows"/>

    <!--

    The <customErrors> section enables configuration

    of what to do if/when an unhandled error occurs

    during the execution of a request. Specifically,

    it enables developers to configure html error pages

    to be displayed in place of a error stack trace.

    -->

    <customErrors mode="Off" defaultRedirect="GenericErrorPage.htm">

    </customErrors>

    <authorization>

    <allow users="miron"/>

    </authorization>

    </system.web>

    </configuration>

     

     

    This is Driving me Crazy.

    Tuesday, April 8, 2008 3:33 PM