when i fire insert query i get SqlException

janakors

Member
Joined
Apr 6, 2020
Messages
5
Programming Experience
Beginner
Hi ,
I am new to this forum and new to C# web programming. I am developing LMS (Library Management system) using SQL Server and Visual Studio ver 16. I have an issue with the insertion of data into the database. the same code I used in other pages and work perfectly. put hardcoded values in insert statement and it worked perfectly which means that there is no issue with connection to database so where is the issue? it can be the text boxes etc name and property which i also checked and found correct. code is as follows
SignUpNewMemeber:
void SignUpNewMember()
        {
          
            try
            {
                SqlConnection con = new SqlConnection(strcon);
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                    
                }
                // SqlCommand cmd = con.CreateCommand();
                //cmd.CommandType = CommandType.Text;// beow is the way to write a query like ist put '' then iside we have "" and then isde we  have ++ and then inside we have text box name
                // cmd.CommandText= "INSERT INTO member_master_tbl values('"++"','"++"','"++"','"++"','"++"','"++"','"++"','"++"','"++"')"
                // cmd.CommandText = "INSERT INTO member_master_tbl (full_name,dob,contact_no,email,state,city,pincode,full_address,member_id,password) values ('" + TextBox1.Text.Trim() + "','" + TextBox2.Text.Trim() + "','"+TextBox4.Text.Trim()+"','" + TextBox3.Text.Trim() + "','" + DropDownList1.SelectedItem.Value + "','" + TextBox6.Text.Trim() + "','" + TextBox7.Text.Trim() + "','" + TextBox5.Text.Trim() + "','" + TextBox8.Text.Trim() + "','" + TextBox9.Text.Trim() + "');";

                //notr thr col nsmes in the query are case sensitive
                String status = "pending";

                SqlCommand cmd = new SqlCommand("INSERT INTO member_master_tbl (full_name,dob,contact_no,email,state,city,pincode,full_address,member_id,password,account_status)values(@full_name,@dob,@contact_no,@email,@state,@city,@pincode,@full_address,@member_id,@password,@account_status);", con);
                cmd.Parameters.AddWithValue("@full_name", TextBox1.Text.Trim());
                cmd.Parameters.AddWithValue("@dob", TextBox2.Text.Trim());
                cmd.Parameters.AddWithValue("@contact_no", TextBox3.Text.Trim());
                cmd.Parameters.AddWithValue("@email", TextBox4.Text.Trim());
                cmd.Parameters.AddWithValue("@state", DropDownList1.SelectedItem.Value);
                cmd.Parameters.AddWithValue("@city", TextBox6.Text.Trim());
                cmd.Parameters.AddWithValue("@pincode", TextBox7.Text.Trim());
                cmd.Parameters.AddWithValue("@full_address", TextBox5.Text.Trim());
                cmd.Parameters.AddWithValue("@member_id", TextBox8.Text.Trim());
                cmd.Parameters.AddWithValue("@password", TextBox9.Text.Trim());
                cmd.Parameters.AddWithValue("@account_status",status);
                
                cmd.ExecuteNonQuery();
                con.Close();
                Response.Write("<script>alert('Sign Up Successful. Go to User Login to Login');</script>");
            }
            catch (SqlException  ex)
            {
                
                Response.Write("<script>alert('" + ex.Message + "');</script>");
            }
        }
C#:
 
Well, I have changed the browser and it worked. I was using chrome and now used Edge but I didn't really understand why. please comment
 
I would recommend using a asp.net update panel with ajax or an inline js function to avoid browser conflicts.

You should also consider naming your controls with appropriate names that make sense to identifying their purpose and relationship on your form.

None of what you said actually makes sense. If you used the exact same code, then it would execute the same in any other browser as it did before. So clearly something is different.
 
I did it. what I needed at that time that how to print the detailed exception which I found and apply and then it told me that the size of the one of tables field cant hold more then 50 char so increase that and all are fine
 
Note something about table sizes. When you set a table size to use 50 chars, but lets say you only use 15 of them during an insert command. You're still using the space for 50 chars regardless of only filling 15 chars in that field. This is to do with how the structures are defined, so only increase field capacity to that of what you really need, because each command which fills a new row of data will accumulate this space, regardless of whether it's used or not. It also helps to check the fields before inserting data, but this can be simplified by using ORM's.
 
Back
Top Bottom