Click here to Skip to main content
15,910,471 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i am giving small downgraded version of my code but please help me to identify the bug.a detailed explanation would be help full.what i am doing is i am storing selected value from a drop down list into an variable . now when i click button the value in variable must get stored in database but what is happening that when i click on button i get this error
The parameterized query '(@value nvarchar(4000))insert into abs2(vab2)values(@value)' expects the parameter '@value', which was not supplied.

when i debug i found value of variable to be always null can anybody tell me whats happening

here is my code.please tell where i went wrong and what should i do and why ithappens?
first markup
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1"  runat="server">
    <div>
      <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
       onselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="112px">
            <asp:ListItem>value1</asp:ListItem>
            <asp:ListItem>value2</asp:ListItem>
            <asp:ListItem>value3</asp:ListItem>
            <asp:ListItem>value4</asp:ListItem>
        </asp:DropDownList>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
      </div>
    </form>
</body>
</html>

C#
public partial class _Default : System.Web.UI.Page
{
    string var;
    SqlConnection conn;
    SqlCommand cmd;
    string str = ConfigurationManager.ConnectionStrings["cnstr"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        var = DropDownList1.SelectedValue;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        conn = new SqlConnection(str);
        cmd = new SqlCommand("insert into abs2(vab2)values(@value)",conn);
        cmd.Parameters.AddWithValue("@value", var);
        try
        {
            conn.Open();
            int a = cmd.ExecuteNonQuery();
            if (a > 0)
            {
                Response.Write("inserted");
            }
            else
            {
                Response.Write("failed");
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }
}
Posted
Updated 2-Jan-12 20:10pm
v2

SQL
cmd = new SqlCommand("insert into abs2(vab2)values(@vab2)",conn);
cmd.Parameters.AddWithValue("@value", var);


you can insert direct by passing var to query
 
Share this answer
 
v2
declare variable like
C#
var v= DropDownList1.SelectedValue;

cmd=new SqlCommand("insert into abs2(vab2)values(@vab2)",conn);
cmd.Parameters.AddWithValue("@value", v);
 
Share this answer
 
v2
Try this one

SQL
cmd = new SqlCommand("insert into abs2(vab2)values(@value)",conn);
cmd.Parameters.AddWithValue("@value", DropDownList1.SelectedValue);
 
Share this answer
 
string var;


You cannot use var as variable name ; var is a reserved word.
 
Share this answer
 
VALUE is reserved key word. Please use any other variable name

http://msdn.microsoft.com/en-us/library/aa238507(v=sql.80).aspx[^]


C#
conn = new SqlConnection(str);
cmd = new SqlCommand("insert into abs2(vab2)values(@value1)",conn);
cmd.Parameters.AddWithValue("@value1", var);


Thanks
Vinod
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900