-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.

lupinx2
  • 83
  • 1
  • 7
  • 1
    Detail of the specific exception would be useful. If you breakpoint on the catch, and then view the details it will help. – Barry O'Kane Feb 28 '22 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. – David Feb 28 '22 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. – Jamiec Feb 28 '22 at 16:44
  • Does the exception have any kind of message? It should at least tell you whether it's a timeout or a case of invalid input – Andrew Corrigan Feb 28 '22 at 16:46
  • 2
    As a complete aside: Dont store passwords in plain text! – Jamiec Feb 28 '22 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" – Jamiec Feb 28 '22 at 16:52
  • Thank you everyone! I learned a lot today about inner exceptions and breakpoints and what the "Watch" tab in visual studio does. for posterity the inner exception was: {"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\"."} which as Jamiec pointed out was me being dumb and not noticing I declared val1 multiple times. Sleep is important! – lupinx2 Feb 28 '22 at 17:29

1 Answers1

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.

lupinx2
  • 83
  • 1
  • 7