来自文本框的用户输入在数据库中保存为空值 [英] User input from textbox saving as null values in database

查看:84
本文介绍了来自文本框的用户输入在数据库中保存为空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我的程序将立即为我的数据库表的两列接受多个输入。到目前为止,我还没能发现我的代码有任何问题。但是,正在将空值添加到我的数据库中,而不是用户添加的值。请帮助



我尝试过:



Hello,
My program is going to accept multiple inputs at once for two columns of my database table. I have'nt been able to detect any problem with my code so far. However, null values are being added to my database in place of values added by the user. Help please

What I have tried:

<pre><pre><pre lang="c#">

protected void SaveBtn_Click(object sender, EventArgs e)
        {
            //save coursecodes and units entered to database
            try
            {

                using (SqlConnection sqlCon = new SqlConnection(connectionString))
                {

                        sqlCon.Open();

                    string Query = "INSERT INTO Courses (courseCode,courseUnit) VALUES (@courseCode,@courseUnit)";
                    SqlCommand sqlCmd = new SqlCommand(Query, sqlCon);

                    List<KeyValuePair<object, object>> lst = new System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<object, object>>();


                    

                    <pre>                lst.Add(new System.Collections.Generic.KeyValuePair<object, object>((FindControl("TextBox1")) ?? (object)DBNull.Value, (FindControl("TextBox3")) ?? (object)DBNull.Value));
                lst.Add(new System.Collections.Generic.KeyValuePair<object, object>((FindControl("TextBox2")) ?? (object)DBNull.Value, (FindControl("TextBox4")) ?? (object)DBNull.Value));
                lst.Add(new System.Collections.Generic.KeyValuePair<object, object>((FindControl("TextBox7")) ?? (object)DBNull.Value, (FindControl("TextBox8")) ?? (object)DBNull.Value));
                lst.Add(new System.Collections.Generic.KeyValuePair<object, object>((FindControl("TextBox9")) ?? (object)DBNull.Value, (FindControl("TextBox10")) ?? (object)DBNull.Value));
                lst.Add(new System.Collections.Generic.KeyValuePair<object, object>((FindControl("TextBox13")) ?? (object)DBNull.Value, (FindControl("TextBox14")) ?? (object)DBNull.Value));
                lst.Add(new System.Collections.Generic.KeyValuePair<object, object>((FindControl("TextBox15")) ?? (object)DBNull.Value, (FindControl("TextBox16")) ?? (object)DBNull.Value));
                lst.Add(new System.Collections.Generic.KeyValuePair<object, object>((FindControl("TextBox19")) ?? (object)DBNull.Value, (FindControl("TextBox20")) ?? (object)DBNull.Value));
                lst.Add(new System.Collections.Generic.KeyValuePair<object, object>((FindControl("TextBox21")) ?? (object)DBNull.Value, (FindControl("TextBox22")) ?? (object)DBNull.Value));
                lst.Add(new System.Collections.Generic.KeyValuePair<object, object>((FindControl("TextBoX25")) ?? (object)DBNull.Value, (FindControl("TextBox26")) ?? (object)DBNull.Value));
                lst.Add(new System.Collections.Generic.KeyValuePair<object, object>((FindControl("TextBox27")) ?? (object)DBNull.Value, (FindControl("TextBox28")) ?? (object)DBNull.Value));
                lst.Add(new System.Collections.Generic.KeyValuePair<object, object>((FindControl("TextBox31")) ?? (object)DBNull.Value, (FindControl("TextBox32")) ?? (object)DBNull.Value));
                lst.Add(new System.Collections.Generic.KeyValuePair<object, object>((FindControl("TextBox33")) ?? (object)DBNull.Value, (FindControl("TextBox34")) ?? (object)DBNull.Value));
                lst.Add(new System.Collections.Generic.KeyValuePair<object, object>((FindControl("TextBox37")) ?? (object)DBNull.Value, (FindControl("TextBox38")) ?? (object)DBNull.Value));
                lst.Add(new System.Collections.Generic.KeyValuePair<object, object>((FindControl("TextBox39")) ?? (object)DBNull.Value, (FindControl("TextBox40")) ?? (object)DBNull.Value));
                lst.Add(new System.Collections.Generic.KeyValuePair<object, object>((FindControl("TextBox5")) ?? (object)DBNull.Value, (FindControl("TextBox6")) ?? (object)DBNull.Value));
                lst.Add(new System.Collections.Generic.KeyValuePair<object, object>((FindControl("TextBox11")) ?? (object)DBNull.Value, (FindControl("TextBox12")) ?? (objec"))));

                    foreach (var item in lst)
                    {
                        if (item.Key != null && item.Value != null)
                        {
                            sqlCmd.Parameters.Clear();
                            sqlCmd.Parameters.AddWithValue("@courseCode", item.Key);
                            sqlCmd.Parameters.AddWithValue("@courseUnit", item.Value);
                        }
                        sqlCmd.ExecuteNonQuery();
                    }
                    lblSuccessMessage.Text = "Courses successfully added";
                        lblErrorMessage.Text = "";

                   
                }
            }
                catch (Exception ex)
            {

                lblSuccessMessage.Text = "";
                lblErrorMessage.Text = ex.Message;
            }


            /*  //use coursecode input to name new columns in gridview
              foreach (var c in GridView1.HeaderRow.Cells)
              {

              }
              */
}
        }

推荐答案

I删除了FindControl()

我的最终代码,



I removed the FindControl()
my final code,

<pre>     protected void SaveBtn_Click(object sender, EventArgs e)
        {
            //save coursecodes and units entered to database
            try
            {
                using (SqlConnection sqlCon = new SqlConnection(connectionString))
                {

                    sqlCon.Open();
                    string Query = ("INSERT INTO Courses (courseCode,courseUnit) VALUES (@courseCode,@courseUnit)");
                    SqlCommand sqlCmd = new SqlCommand(Query, sqlCon);

                    //working single pair
                    //sqlCmd.Parameters.AddWithValue("@courseCode", courseCodeTB.Text);
                    //sqlCmd.Parameters.AddWithValue("@courseUnit", courseUnitTB.Text);
                    //sqlCmd.ExecuteNonQuery();
                    //lblSuccessMessage.Text = "Courses successfully added";
                    //lblErrorMessage.Text = "";



                    List<KeyValuePair<object, object>> lst = new System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<object, object>>();

                    lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox1.Text ?? (object)DBNull.Value, TextBox3.Text ?? (object)DBNull.Value));
                    lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox2.Text ?? (object)DBNull.Value, TextBox4.Text ?? (object)DBNull.Value));
                    lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox7.Text ?? (object)DBNull.Value, TextBox8.Text ?? (object)DBNull.Value));
                    lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox9.Text ?? (object)DBNull.Value, TextBox10.Text ?? (object)DBNull.Value));
                    lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox13.Text ?? (object)DBNull.Value, TextBox14.Text ?? (object)DBNull.Value));
                    lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox15.Text ?? (object)DBNull.Value, TextBox16.Text ?? (object)DBNull.Value));
                    lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox19.Text ?? (object)DBNull.Value, TextBox20.Text ?? (object)DBNull.Value));
                    lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox21.Text ?? (object)DBNull.Value, TextBox22.Text ?? (object)DBNull.Value));
                    lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox25.Text ?? (object)DBNull.Value, TextBox26.Text ?? (object)DBNull.Value));
                    lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox27.Text ?? (object)DBNull.Value, TextBox28.Text ?? (object)DBNull.Value));
                    lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox31.Text ?? (object)DBNull.Value, TextBox32.Text ?? (object)DBNull.Value));
                    lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox33.Text ?? (object)DBNull.Value, TextBox34.Text ?? (object)DBNull.Value));
                    lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox37.Text ?? (object)DBNull.Value, TextBox38.Text ?? (object)DBNull.Value));
                    lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox39.Text ?? (object)DBNull.Value, TextBox40.Text ?? (object)DBNull.Value));
                    lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox5.Text ?? (object)DBNull.Value, TextBox6.Text ?? (object)DBNull.Value));
                    lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox11.Text ?? (object)DBNull.Value, TextBox12.Text ?? (object)DBNull.Value));


                    foreach (var item in lst)
                    {
                        if (item.Key != null && item.Value != null)
                        {
                            sqlCmd.Parameters.Clear();
                            sqlCmd.Parameters.AddWithValue("@courseCode", item.Key);
                            sqlCmd.Parameters.AddWithValue("@courseUnit", item.Value);
                        }
                        sqlCmd.ExecuteNonQuery();
                    }
                    lblSuccessMessage.Text = "Courses successfully added";


                   
                }
            }
                catch (Exception ex)
            {

                lblSuccessMessage.Text = "";
                lblErrorMessage.Text = ex.Message;
            }


            /*  //use coursecode input to name new columns in gridview
              foreach (var c in GridView1.HeaderRow.Cells)
              {

              }
              */

        }


这篇关于来自文本框的用户输入在数据库中保存为空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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