Home > Enterprise >  Binding Textbox.Text to Service-Based DataBase
Binding Textbox.Text to Service-Based DataBase

Time:08-30

I'm creating a money flow program in Winform. Now i have to bind textbox to Service-Based DataBase (LocalDB) but it gives error (System.Data.SqlClient.SqlException: 'Incorrect syntax near ')'.').I'm not fimiliar with databases so i don't know how to code but im using this

        private void addButton_Click(object sender, EventArgs e)
        {
            if (ComboBox1.SelectedIndex == 1)
            {                                           
                    SqlConnection sc = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\dotex\Desktop\tst\Manager\Manager\ManagerFile\MoneyTransactionsFile\MoneyMovements\MoneyMovementDataBase\moneydatabase.mdf;Integrated Security=True");
                    sc.Open();
                    SqlCommand sd = new SqlCommand("insert into Name(@AddedMoney)", sc);
                    sd.Parameters.AddWithValue("@AddedMoney", TextBox.Text);
                    sd.ExecuteNonQuery();// this line gives the error
                    sc.Close();
            }
             Else
             {
             
             }

This is a photo of my database for now. photo If u need any information to fix just write comment.

CodePudding user response:

This is a straight SQL question, unrelated to the application or the TextBox.

When you write an INSERT statement in SQL, you can omit the list of column names if and only if you provide a value for every column in the correct order. Your table has two columns but you are providing only one value, so you it won't work. You need to specify the column you want to insert into and the other column will be set to its default value. If you do omit the column list, you still need to include the VALUES keyword.

You are also specifying Name as the name of the table but, in your screenshot, it's actually named Table. Both are atrocious names and you should change it to something that indicates the purpose of the table.

INSERT INTO MyWellNamedTable (AddedMoney) VALUES (@AddedMoney)
  • Related