通过组合框插入值 [英] inserting value by combobox

查看:71
本文介绍了通过组合框插入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从combobox插入值,但是出现一些错误,我是新手,所以对.net不太了解
这是我的代码

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");
           }

推荐答案

只是一个简单的错字.
在您的行中cmd.Parameters.Add("@sec_questions", SqlDbType.VarChar).Value =comboBox1.Text;
您将@sec_question拼写为@sec_questions
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


错误表示它的意思.如果您无法解决这个问题,那么您应该研究一些更基本的示例.显然,这不是生产质量代码,没有人应该为此付出代价,因此,我假设您正在四处学习.从较小的小块开始,您可以理解.首先学习C#,然后再担心SQL和数据库.

该错误表示它的意思.您创建了一个类似SQL的实例,该实例期望一个名为sec_question的参数.您没有提供.您提供了sec_questions.
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.


这篇关于通过组合框插入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆