hii all i am new in asp.net.i am trying to implement a grid view with all functionalities(insert,update delete).
i have successfullly done edit,update and cancel.but not able to insert(new record) in grid view.As soon as i click on insert i get exception as:
Exception Details: System.Data.SqlClient.SqlException: Must declare the scalar variable "@EmployeeNo".Means i need to declare this in my c# page but how???my code for inserting is:

in my database i have columns as :
EmployeeNO(int),Name(nvarchar),Nick(nvarchar)

thankzzz

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Insert"))
        {
            TextBox txtNewEmployeeNo = (TextBox)GridView1.FooterRow.FindControl("txtNewEmployeeNo");
            TextBox txtNewName = (TextBox)GridView1.FooterRow.FindControl("txtNewName");
            TextBox txtNewNick = (TextBox)GridView1.FooterRow.FindControl("txtNewNick");
            // and ur insert code goes here.....
            String ConnectionString = "server=alok-pc;database=db_test;user id=sa;password=alokamar123456;";
            SqlConnection Conn = new SqlConnection(ConnectionString);
            Conn.Open();
            SqlDataAdapter da = new SqlDataAdapter("insert into tab1(EmployeeNo,Name,Nick) values (@EmployeeNo,@Name,@Nick)", Conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "emp");
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
            Conn.Close();

        }
    }

Recommended Answers

All 2 Replies

everything loks great but i think
you forgot to specify the list of parameters..for @EmployeeNo..

I believe you also need to set the Parameters property in below way for all the parameters you are passing in Insert query:
da.InsertCommand.Parameters.AddWithValue("@EmployeeNo",txtNewEmployeeNo.text)

Also you must need to update the your ds(dataset) to make changes in database also. so you need to call update method of data adapter also.
ds.Update(ds)
===================================================================

hii all i am new in asp.net.i am trying to implement a grid view with all functionalities(insert,update delete).
i have successfullly done edit,update and cancel.but not able to insert(new record) in grid view.As soon as i click on insert i get exception as:
Exception Details: System.Data.SqlClient.SqlException: Must declare the scalar variable "@EmployeeNo".Means i need to declare this in my c# page but how???my code for inserting is:

in my database i have columns as :
EmployeeNO(int),Name(nvarchar),Nick(nvarchar)

thankzzz

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Insert"))
        {
            TextBox txtNewEmployeeNo = (TextBox)GridView1.FooterRow.FindControl("txtNewEmployeeNo");
            TextBox txtNewName = (TextBox)GridView1.FooterRow.FindControl("txtNewName");
            TextBox txtNewNick = (TextBox)GridView1.FooterRow.FindControl("txtNewNick");
            // and ur insert code goes here.....
            String ConnectionString = "server=alok-pc;database=db_test;user id=sa;password=alokamar123456;";
            SqlConnection Conn = new SqlConnection(ConnectionString);
            Conn.Open();
            SqlDataAdapter da = new SqlDataAdapter("insert into tab1(EmployeeNo,Name,Nick) values (@EmployeeNo,@Name,@Nick)", Conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "emp");
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
            Conn.Close();

        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.