Home > Back-end >  Trying to insert SQL row from C#, throws exception: 'System.Data.SqlClient.SqlException'
Trying to insert SQL row from C#, throws exception: 'System.Data.SqlClient.SqlException'

Time:03-04

I'm very much a novice with C# to SQL interaction so apologies if the problem here is obvious or a duplicate. I'm trying to insert a new row into the table 'Clientes', the first part of this code connects to the database, then checks the table for duplicates, I know those parts work. I included it just in case maybe the problem maybe comes from my connection string or something.

Once it gets to the Try Catch, it throws up the "Error" message I put in there, so I know the failure is happening while inserting. Usually I can work things like this out based on info from the error message but this is only giving me
[Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll]
in the output tab, nothing in the error list and no further details that I can find, and I've been unable to deduce the problem based on similar SO posts.

               if ( textBox_tel.Text.All(c => char.IsDigit(c))) //checks no letters in phone number
            {
                string connectionstring;
                SqlConnection con;
                connectionstring = @"Data Source = DRAGONSLAYER;Initial Catalog=bancodb;User id=bancodb_admin;Password=admin";
                con = new SqlConnection(connectionstring);
                con.Open(); //now connected to the DB

                string querysignupsubmitcheck = "Select * from Clientes Where Login = '"   textBox_usr.Text   "'";
                SqlDataAdapter sda_signupsubmitcheck = new SqlDataAdapter(querysignupsubmitcheck, con);
                DataTable dtbl_signupsubmitcheck = new DataTable();
                sda_signupsubmitcheck.Fill(dtbl_signupsubmitcheck);
                con.Close();
                if (dtbl_signupsubmitcheck.Rows.Count < 1) //checks the new client row isn't a duplicate
                {
                    try
                    {
                        string querysignupsubmit = "Insert into Clientes (Nombre, Telefono, Login, Password) Values ("  
                                                    textBox_name.Text   ", "  
                                                    textBox_tel.Text   ", "  
                                                    textBox_usr.Text   ", "  
                                                    textBox_pword2.Text   ")";
                        SqlCommand sc_signupsubmitc = new SqlCommand(querysignupsubmit, con);
                        sc_signupsubmitc.ExecuteNonQuery();
                        this.Close();
                        objform_login.Show();
                    }
                    catch { label_alert.Text = "ERROR DE BASE DE DATOS"; }
                }
                else
                {
                    label_alert.Text = "usuario ya existe";
                }
            }
            else
            {
                label_alert.Text = "Telefono acepta solo numeros";
            }

based on something suggested on output error list

EDIT2: I am dumb and declared Val1 multiple times, dumb dumb. Thanks for all the help y'all.

CodePudding user response:

I added a breakpoint (right click inside a pair of brackets) to my Catch statement as @Jamiec suggested. Then, while poking around with the debugging tabs, I found on the Watch tab to the left of the output I can keep track of a value in realtime. So I added the ex exception to the Watch and this message came up:

{"The variable name '@val1' has already been declared. Variable names must be unique within a query batch or stored procedure.\r\nMust declare the scalar variable "@val2"."}

I had accidentally declared val1 like four times in a row in my SqlCommand and somehow failed to notice it on multiple read-throughs.

  • Related