Member 13331557 Ответов: 1

Пользовательский ввод из текстового поля сохранение в виде нулевых значений в базе данных


Привет,
Моя программа будет принимать сразу несколько входных данных для двух столбцов моей таблицы базы данных. До сих пор мне не удалось обнаружить никаких проблем с моим кодом. Однако нулевые значения добавляются в мою базу данных вместо значений, добавленных пользователем. Помогите пожалуйста

Что я уже пробовал:

<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)
              {

              }
              */
}
        }

Kornfeld Eliyahu Peter

DBNull и null-это не одно и то же...

Member 13331557

Вместо значений, вводимых в текстовые поля, "null" сохраняется в БД

1 Ответов

Рейтинг:
6

Member 13331557

Я удалил FindControl()
мой последний код,

<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)
              {

              }
              */

        }