2

Context: I'm developing an app for windows in Visual Studio that has a table of stock materials and another of buyed materials, both in a Sql Server. I want that every time you buy something it is added into the stock table.

I'm new in using SQL with c# combined.

I'm trying this from a tutorial, but does nothing. Not even an exception.

            string cmdString = "Insert INTO Table1 (Column_name) VALUES (@val1)";
            string connString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True";
            using (SqlConnection conn = new SqlConnection(connString))
            {
                using (SqlCommand comm = conn.CreateCommand())
                {

                    comm.Connection = conn;
                    comm.CommandText = cmdString;                    
                    comm.Parameters.AddWithValue("@val1", Value);
                    try
                    {
                        conn.Open();
                        comm.ExecuteNonQuery();
                        conn.Close()
                    }
                    catch(SqlException ex)
                    {
                        
                    }
                }
            }

Is this totally wrong or should i change something?

Edit: I figured out. I was inserting val1 in a column, but the ID was empty so it throws an NullId exception. For some reason in debug mode I wasn't able to see it.

Thanks for the help. If I have the Table1 with autoincrement why it needs an ID? There is a way that when something is inserted the Id generates automatically?

raulmd13
  • 201
  • 2
  • 10
  • Have you inserted anything into the database? – Saeid Amini Oct 05 '20 at 10:39
  • You should probably do something in the catch block. Or did you mean you never end up in the catch block? – Juho Rutila Oct 05 '20 at 10:45
  • ["AddWithValue is Evil"](https://www.dbdelta.com/addwithvalue-is-evil/) – sticky bit Oct 05 '20 at 10:47
  • You'd be better off having a trigger execute on data insert and do the insert into the other table. – WSC Oct 05 '20 at 10:48
  • It never ends in the cacth block. The database is empty because every time that you compile in visual studio the database resets. – raulmd13 Oct 05 '20 at 10:56
  • Every time that you compile in Visual Studio the database resets?!!! Why happen something like this? – Saeid Amini Oct 05 '20 at 11:10
  • 3
    Does this answer your question? [Why saving changes to a database fails?](https://stackoverflow.com/questions/17147249/why-saving-changes-to-a-database-fails) – SMor Oct 05 '20 at 11:33
  • No, I was already aware of that issue. Anyone see anything wrong in the code? – raulmd13 Oct 05 '20 at 17:18
  • Didn't you catch exception? Is `Column_name` a real field in your Table1? – 大陸北方網友 Oct 06 '20 at 02:47
  • You can't add another follow up question to your post and expect a new answer, when you do this it has the potential to invalidate previous answers and comments which later will lead to your post being closed and you may not attract a useful response. Post your additional question as a new post, it is an interesting topic that many people starting out get wrong. – Chris Schaller Oct 07 '20 at 02:51
  • You are right. I'm sorry. – raulmd13 Oct 07 '20 at 06:16

1 Answers1

1

You can use this query to insert data like that :

{
    if (con.State == ConnectionState.Open)
        con.Close();
}   
{
    SqlCommand cmd0561 = new SqlCommand(@"insert into Table1 (value1,value1) values  
    (@value1,@timee)", con);
    cmd0561.Parameters.AddWithValue("@value1", value1.Text.Trim);
    cmd0561.Parameters.AddWithValue("@value2", value2.Text.Trim);
    con.Open();
    cmd0561.ExecuteNonQuery();
    con.Close();
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459