-2

I'm building a C# Windows Forms database application. Data should be written in a text field and saved in a table using a button. My problem is that the data is not saved in the table.

I have built a list that should show the records of the table. When you click on the insert button, the text is displayed in the list, but it is not saved in the table.

My code:

        private void insert_Click(object sender, EventArgs e)
    {
        string con_string = Properties.Settings.Default.DB1_ConnectionString;
        SqlConnection con = new SqlConnection(con_string);
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into tab_Musik values (@Titel,@Inerpret,@Genre)", con);
        cmd.Parameters.AddWithValue("@Titel", int.Parse(txt_Titel.Text));
        cmd.Parameters.AddWithValue("@Inerpret", txt_Interpret.Text);
        cmd.Parameters.AddWithValue("@Genre", double.Parse(kmb_Genre.Text));
        cmd.ExecuteNonQuery();
        con.Close();

    }

    private void show[enter image description here][1]_Click(object sender, EventArgs e)
    {
        string con_string = Properties.Settings.Default.DB1_ConnectionString;
        SqlConnection con = new SqlConnection(con_string);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from tab_Musik", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        dataGridView1.DataSource=dt;
        
    }

enter image description here enter image description here

  • 6
    Just as a side note... Your code is vulnerable to [SQL Injection](https://stackoverflow.com/questions/35163361/how-can-i-add-user-supplied-input-to-an-sql-statement) – Guilherme Sep 05 '22 at 19:30
  • 2
    What's the value of `return intResult;`? If it's not zero, it worked. Make sure you are looking at the right database, and not a copy. – LarsTech Sep 05 '22 at 19:32
  • 3
    Side note: you are missing `using` on those Sql objects, which need to be disposed – Charlieface Sep 05 '22 at 19:37
  • Since you are using a DataAdapter, you ought to know that it can and will do virtually everything for you including maintaining and managing the connection, creating and remembering Insert, Update and Delete SQL commands and avoiding the many many problems from gluing data into strings to make queries. It can even track whioch data has been changed, inserted or deleted(!) Mosey on over to the Microsoft documentation and delve into some of the articles there to learn how to use those tools properly. – Ňɏssa Pøngjǣrdenlarp Sep 05 '22 at 20:00
  • A SQL command has four types 1) Select 2) Insert 3) Update 4) Delete. For a datatable update to work you must have all four type loaded. A command builder takes a select command and automatically creates the other three types. See : https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommandbuilder?view=dotnet-plat-ext-6.0 – jdweng Sep 05 '22 at 22:06
  • If you want to help, please be able to show your interface design and database design. I believe this will solve the problem better. – Lei Zhang-MSFT Sep 07 '22 at 09:40
  • First of all I would like to thank you for the replies. I tried your tips but unfortunately didn't quite understand them because I'm learning everything new. I shortened and changed my code a bit: – oeztuerk_42 Sep 11 '22 at 21:49
  • you can see my new code on the top (i edit my question), i also addet two pictures. One show my table and the other one shows the design of the form – oeztuerk_42 Sep 11 '22 at 22:04

1 Answers1

0

For the problem you described about clicking the insert button, the data cannot be saved to the table, you can try the following code:

private void btnAdd_Click(object sender, EventArgs e)
    {            
        string con_string = "database connection string";
        SqlConnection sqlConnection = new SqlConnection(con_string);           
        string myinsert = "insert into tab_Musik values (@Titel,@Inerpret)";
        SqlCommand mycom = new SqlCommand(myinsert, sqlConnection);
        mycom.Parameters.AddWithValue("@Titel", textBox1.Text);
        mycom.Parameters.AddWithValue("@Inerpret", textBox2.Text);
        sqlConnection.Open();
        mycom.ExecuteNonQuery();
        mycom.Clone();
        mycom.Dispose();}

Show results:

enter image description here

enter image description here

Lei Zhang-MSFT
  • 280
  • 1
  • 5
  • I tried your code, unfortunately it doesn't work either. I think it has nothing to do with the code for me it seems like a server problem. Because when I look at the list, the inserted data are displayed there but they are not saved in the table. – oeztuerk_42 Sep 13 '22 at 14:40
  • Inserting the textbox value into the database table is achievable using the code. When you click Add Musik, is there not data displayed in the table and no error is reported? You are using Data Sources to connect, you can use the database to create a table to connect. – Lei Zhang-MSFT Sep 15 '22 at 08:02