Trying to insert SQL row from C#, throws exception: 'System.Data.SqlClient.SqlException'

510 Views Asked by At

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 another question here, I changed the code inside the try-catch statement to this, but it still throws the same exception:

                     using (con)
                        {
                         string querysignupsubmit = "INSERT INTO Clientes (Nombre, Telefono, Login, Password) VALUES (@val1, @val2, @val3, @val4)";
                            using (SqlCommand sc_signupsubmit = new SqlCommand())
                            {
                                sc_signupsubmit.Connection = con;
                                sc_signupsubmit.CommandText = querysignupsubmit;
                                sc_signupsubmit.Parameters.AddWithValue("@val1", textBox_name.Text);
                                sc_signupsubmit.Parameters.AddWithValue("@val1", textBox_tel.Text);
                                sc_signupsubmit.Parameters.AddWithValue("@val1", textBox_usr.Text);
                                sc_signupsubmit.Parameters.AddWithValue("@val1", textBox_pword2.Text);
                                con.Open();
                                sc_signupsubmit.ExecuteNonQuery();
                                con.Close();
                                this.Close();
                                objform_login.Show();
                            }
                         }

Any help or suggestions are appreciated, this is the code for the table I'm trying to insert into:

CREATE TABLE [dbo].[Clientes] (
[ClienteID] INT           IDENTITY (1, 1) NOT NULL,
[Nombre]    VARCHAR (255) NOT NULL,
[Telefono]  VARCHAR (20)  NOT NULL,
[Login]     VARCHAR (255) DEFAULT ('default_login') NOT NULL,
[Password]  VARCHAR (128) NOT NULL,
CONSTRAINT [PK_Clientes] PRIMARY KEY CLUSTERED ([ClienteID] ASC)
);

EDIT: Here is the full output and error list tabs, the exit message is from me closing it output error list

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

1

There are 1 best solutions below

0
On BEST ANSWER

I added a breakpoint (right click inside a pair of brackets if you're using VSC) 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.