Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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)
              {

              }
              */
}
        }
Posted
Updated 8-Oct-17 1:52am
v2
Comments
Kornfeld Eliyahu Peter 1-Oct-17 5:13am    
DBNull and null are not the same...
Member 13331557 1-Oct-17 7:18am    
Instead of values inputted into textboxes, "null" is saved to DB

1 solution

I removed the FindControl()
my final code,

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

              }
              */

        }
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900