如何在基于窗口的应用程序中将checkboxlist的值插入sql数据库[C#] [英] how to insert the values of checkboxlist to sql database in windowbased application[C#]

查看:59
本文介绍了如何在基于窗口的应用程序中将checkboxlist的值插入sql数据库[C#]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,
我的编码是

Sir,
My coding is

public partial class Form1 : Form
    {
        SqlConnection cn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=Q;Integrated Security=True");
        public Form1()
        {
            InitializeComponent();
        }
        string s;

        private void btnSubmit_Click(object sender, EventArgs e)
        {

            if(rbtMale.Checked==true)
            {
                s="Male";
            }
            else
            {
                s="Female";
            }
            for (int i = 0; i < checkedListBox1.Items.Count - 1;i++ )
            {
                cn.Open();
                SqlCommand cmd = new SqlCommand("insert into registration values('" + txtname.Text + "','" + s + "','" + comboBox1.SelectedText + "','" + comboBox1.SelectedItem.ToString() + "','" + listBox1.SelectedItem.ToString() + "','" + checkedListBox1.SelectedValue.ToString()+ "')", cn);
                cmd.ExecuteNonQuery();
                cn.Close();
            }

        }
    }



但是它显示了错误:对象引用未设置为对象的实例.



but it is showing the error:Object reference not set to an instance of an object.

推荐答案

好,我建议买一本书并阅读.您的代码是一场灾难.首先,如果用户可以输入输入到SQL的文本,则他们可以擦除您的数据库.其次,表示层中的SQL代码总是很糟糕.第三,如果您对编程没有足够的了解就无法告诉我们,我们将无法告诉您错误是什么.哪一行有错误?哪个对象为null?您为什么期望它不为null?您是否已遍历代码?

一旦将其清理到生产标准,我将使用带有OpenXML的存储过程在单个DB调用中存储任意数量的数据.

哦,您考虑过使用合理的变量名吗? comboBox1与comboBox2有何不同?你怎么知道是做什么的?

如果要更改数据源怎么办?那行代码会出现在每个处理数据的类中吗?为什么不将其存储在中央位置?
Well, I suggest buying a book and reading it. Your code is a disaster. First of all, if the user can enter text that gets turned in to SQL, they can erase your database. Second, SQL code in the presentation layer is always bad. Third, we can''t tell you what the error is, without you knowing enough about programming to know what to tell us. What line has the error ? What object is null ? Why did you expect it to not be null ? Have you stepped through the code ?

Once you clean this up to a production standard, I''d use a stored proc with OpenXML to store an arbitrary number of bits of data in a single DB call.

Oh, have you considered using sane variable names ? How is comboBox1 different from comboBox2 ? how do you know which does what ?

What if you want to change your data source ? Will that line of code occur in every class that handles data ? Why not store it in a central location ?


很可能是,组合框或列表框选择的项目为空,并且当您尝试将其转换为字符串时.他们抛出一个错误.
Most probably, the combobox or listbox selected items are null and when you try to convert them to strings. they throw an error.




您将值绑定到复选框,组合框的位置,可能就是问题所在


where you are binding value to check box, combo box, may that was the issue


这篇关于如何在基于窗口的应用程序中将checkboxlist的值插入sql数据库[C#]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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