Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have some problem in this code............

C#
cmd.Parameters.Add(new SqlParameter("@dept",comboBox_dept.SelectedValue));
Posted
Updated 5-May-12 20:33pm
v2
Comments
Sandeep Mewara 6-May-12 1:30am    
'What problem'?
Uday P.Singh 6-May-12 2:34am    
what is the error?

It is not mentioned in the question what error occurred in using the above code. However, I think it is better to use
C#
command.Parameters.AddWithValue("@dept", comboBox_dept.SelectedValue);

as explained here
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx[^]
 
Share this answer
 
Comments
Ranjith Reddy CSE 6-May-12 1:56am    
ok use this ?

SelectedValue.Text);
Maciej Los 11-May-12 2:25am    
Good answer, my 5!
VJ Reddy 11-May-12 3:22am    
Thank you, losmac.
Hi ,
Check this
C#
private void button1_Click(object sender, EventArgs e)
     {

         using (SqlConnection con =
                new SqlConnection(@"Data Source=IT-DEV2\SQLEXPRESS;Initial Catalog=test;Integrated Security=True"))
         {
             con.Open();
             using (SqlCommand cmd = new SqlCommand("INSERT INTO dbo.cate (cate_id) values (@cate_id )", con))
             {
                 cmd.Parameters.AddWithValue("@cate_id", comboBox1.SelectedValue );

                 cmd.ExecuteNonQuery();
             }
         }

     }


Best Regards
M.Mitwalli
 
Share this answer
 
You combined two steps:
C#
cmd.Parameters.Add(new SqlParameter("@dept",SqlDbType.Int));
command.Parameters["@dept"].Value = comboBox_dept.SelectedValue;
 
Share this answer
 
v2

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