Must declare the scalar variable "@productname".?

Posted by Sudheep.grandhe under C# on 11/7/2011 | Points: 10 | Views : 4414 | Status : [Member] | Replies : 11
HI,
it shows the error as
Must declare the scalar variable "@productname".?
SqlCommand cmd = new SqlCommand("insert into tbl_product(productname)values(@productname",con)

Best,
Sudheep.



Responses

Posted by: Blessyjees on: 11/7/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi,

Try like this
string sql="insert into tbl_product (productname)values ('"+productname+"')" 


SqlCommand cmd = new SqlCommand(sql,con)
I assume that "productname" as a local variable that keep the the name of the product.

Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 11/7/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

If i am applying the above code.it will jump to the else part.if i execute the command like "insert into tbl_product (productname)values('" + ProductName + "')".it shows the result as one row affected.in my project debugging it will goes to the else part.shows the message as product not added.what the problem in my code.

private void btn_addproduct_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true");
string sql = "insert into tbl_product (productname)values('" + ProductName + "')";
SqlCommand cmd = new SqlCommand(sql, con);
//SqlCommand cmd = new SqlCommand("insert into tbl_product(productname)values(@productname)",con );
cmd.Parameters.AddWithValue("@productname", txt_product.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Open();
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
MessageBox.Show("product added successfully..");//it wont execute the if part it goes to else part directly
txt_product.Text = "";
}
else
{
MessageBox.Show("product not added");
txt_product.Text = "";
}

}


Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Blessyjees on: 11/7/2011 [Member] Bronze | Points: 25

Up
0
Down
hi,

You are passing the values useing parameters. So comment the code string sql = "insert into tbl_product (productname)values('" + ProductName + "')";
and uncomment the code //SqlCommand cmd = new SqlCommand("insert into tbl_product(productname)values(@productname)",con );


Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 11/7/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

If i did like that same error will raise in my code.
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true");
//string sql = "insert into tbl_product (productname)values('" + ProductName + "')";
//SqlCommand cmd = new SqlCommand(sql, con);
SqlCommand cmd = new SqlCommand("insert into tbl_product(productname)values(@productname)",con );
cmd.Parameters.AddWithValue("@productname", txt_product.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Open();
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
MessageBox.Show("product added successfully..");//it will not execute if block.
txt_product.Text = "";
}
else
{
MessageBox.Show("product not added");
txt_product.Text = "";
}

}

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Blessyjees on: 11/7/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi,

There is no use of sqldtaadapter for insert state. This is used to read a list of data from the db. You can just use cmd. Execute and also check your table using sql server. In this you do not get any value in datatable.

Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 11/7/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

what code i have to put in my application.

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Blessyjees on: 11/7/2011 [Member] Bronze | Points: 25

Up
0
Down
Try this

SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true");
SqlCommand cmd = new SqlCommand("insert into tbl_product(productname)values(@productname)",con );
cmd.Parameters.AddWithValue("@productname", txt_product.Text);
con.Open();
int rowsAffected = command.ExecuteNonQuery();
if (rowsAffected> 0)
{
MessageBox.Show("product added successfully..");//it will not execute if block.
txt_product.Text = "";
}
else
{
MessageBox.Show("product not added");
txt_product.Text = "";
}

}



Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 11/7/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

the above code also wont work.it shows the same message.

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Blessyjees on: 11/7/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi,

Have you checked your table. is data entered ?

Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: hrmtsngh86-12538 on: 11/8/2011 [Member] Starter | Points: 25

Up
0
Down
Try this

SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true");
SqlCommand cmd = new SqlCommand("insert into tbl_product(productname)values(@productname)",con );
cmd.Parameters.Add("@productname",SqlDbType.VarChar, 50).Value =txt_product.Text;
con.Open();
int rowsAffected = command.ExecuteNonQuery();
if (rowsAffected> 0)
{
MessageBox.Show("product added successfully..");//it will not execute if block.
txt_product.Text = "";
}
else
{
MessageBox.Show("product not added");
txt_product.Text = "";
}

}
Harmeet

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: RohiK on: 11/8/2011 [Member] Starter | Points: 25

Up
0
Down
Hey Sudheep , please mark it as answer if you got the solution.

Happy Programming!!
Rohi

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response