Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 15-Nov-12 7:29am
Comments
faisal23 at 16-Nov-12 2:09am
   
@sec_question and @sec_questions difference. Another is you can also use comboBox1.selectedvalue.toString();
shaikh-adil at 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?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
shaikh-adil at 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 at 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 at 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 at 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 at 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?
Christian Graus at 15-Nov-12 13:59pm
   
Bloody hell. The problem is where the ERROR says it is, and where my MESSAGES say it is. 'question' and 'questions' are not the same thing. Read it all again. Do what I told you.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
shaikh-adil at 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?

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

  Print Answers RSS
0 OriginalGriff 383
1 Richard Deeming 180
2 Sergey Alexandrovich Kryukov 171
3 Yogesh Kumar Tyagi 165
4 ChauhanAjay 156
0 Sergey Alexandrovich Kryukov 6,212
1 OriginalGriff 5,498
2 CPallini 2,473
3 Richard MacCutchan 1,627
4 Abhinav S 1,530


Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 15 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100