-1

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.

7
  • 1
    Detail of the specific exception would be useful. If you breakpoint on the catch, and then view the details it will help. Commented Feb 28, 2022 at 16:43
  • 2
    You need to get more information from the exception. The type of the exception doesn't tell you much. You also want to get the error message from the exception, any inner exceptions, any optional additional data, etc. But the main thing is the error message, it is trying to tell you what's wrong. Commented Feb 28, 2022 at 16:43
  • 3
    change this catch { label_alert.Text = "ERROR DE BASE DE DATOS"; } to catch(Exception ex){ label_alert.Text = "ERROR DE BASE DE DATOS"; } and put a breakpoint inside the braces to get more info on the actual exception. Commented Feb 28, 2022 at 16:44
  • 2
    As a complete aside: Dont store passwords in plain text! Commented Feb 28, 2022 at 16:47
  • 1
    Also take a closer look at your lines with AddWithValue in it - I expect the error message is something along the lines of "Parameter @val2 is expected but not supplied" Commented Feb 28, 2022 at 16:52

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.