Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
i am inserting value from combobox but there is some error i am a newbie so dont know much about .net
here is my code
SqlConnection conn = new SqlConnection();
           conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\project\Projects\secquestion and password change with new tarika insert\labelnameinsecondformtry\Database1.mdf;Integrated Security=True;User Instance=True";
           SqlCommand cmd = new SqlCommand();
           cmd.Connection = conn;
           conn.Open();

           try
           {
               cmd.CommandText = "INSERT INTO Table2 (username,password,sec_questions) VALUES (@username,@password,@sec_question)";
               cmd.Parameters.Add("@username", SqlDbType.VarChar).Value =textBox1.Text;
               cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = textBox2.Text;
               cmd.Parameters.Add("@sec_questions", SqlDbType.VarChar).Value =comboBox1.Text;
               cmd.ExecuteNonQuery();//error is coming which is "Must declare the scalar variable @sec_question."
           }
           finally
           {
               conn.Close();
               MessageBox.Show("hello welcome");
           }
Posted
Comments
faisal23 16-Nov-12 2:09am    
@sec_question and @sec_questions difference. Another is you can also use comboBox1.selectedvalue.toString();
shaikh-adil 16-Nov-12 4:25am    
and bro,
my code is only for the combobox which have list. User click the one option which is avalilable if i want user can choose one option but use can also type in combobox and it will be updated in the database. How can i do that? This code is only for option which is available in combo box's list i cant create new string at run time. How can i acchive?

Just a simple typo.
In your line cmd.Parameters.Add("@sec_questions", SqlDbType.VarChar).Value =comboBox1.Text;
you have mispelled @sec_question as @sec_questions
 
Share this answer
 
Comments
shaikh-adil 16-Nov-12 4:29am    
and bro,
my code is only for the combobox which have list. User click the one option which is avalilable if i want user can choose one option but use can also type in combobox and it will be updated in the database. How can i do that? This code is only for option which is available in combo box's list i cant create new string at run time. How can i acchive?
The error means what it says. If you can't work this out, then you should work on more basic examples. This is clearly not production quality code, no-one should be paying for it, so I asume you're playing around to learn. Start smaller, in chunks you can understand. Learn C# first, then worry about SQL and databases.

The error means what it says. You created a like of SQL that expects a parameter called sec_question. You did not provide it. You provided sec_questions.
 
Share this answer
 
Comments
shaikh-adil 15-Nov-12 13:37pm    
sir i am a learner. i am making a basic simple project so first of all i am not coding for payment. i am just learning here.
so how can i do it? the textbox field is inserting propoerly in database but i dont know how to do that with combobbox
Christian Graus 15-Nov-12 13:41pm    
Again, if you don't understand the error message, or my response, then you're not really learning, because you're not understanding what you are doing. I'm not trying to be smart here, I had to learn, too. But, I chose projects to learn that suited my level, so I DID learn. The error is what I said it was. It's clear to me that you're biting off more than you can chew and should take a step back and try to learn in smaller increments, so you really learn, and learn things like how to read error messages, etc.
shaikh-adil 15-Nov-12 13:45pm    
sorry
but your language is notunderstandable
"You created a like of SQL that expects a parameter called sec_question. You did not provide it. You provided sec_questions."
a like of sql???? what is it? and can you help me out in combobox without concentration on error? then only i can understand
Christian Graus 15-Nov-12 13:46pm    
Change cmd.CommandText = "INSERT INTO Table2 (username,password,sec_questions) VALUES (@username,@password,@sec_question)";
to
cmd.CommandText = "INSERT INTO Table2 (username,password,sec_questions) VALUES (@username,@password,@sec_questions)";
and it will work. I get that English is not your first language, and I don't hold that against you. but, my explantion should still have been enough, and again, if it's not, then this is too hard for you right now
shaikh-adil 15-Nov-12 13:55pm    
bro you have given me this
cmd.CommandText = "INSERT INTO Table2 (username,password,sec_questions) VALUES (@username,@password,@sec_questions)";
which is same as mine. then where is the problem?

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