2020-12-30

Microsoft sql sever + c# - save not working

private void button4_Click(object sender, EventArgs e)
    {
        try
        {
            // first table
            SqlConnection cn = new SqlConnection(@"Data Source = DESKTOP - MO4HHVS; Initial Catalog = MechanicProject; Integrated Security = True");
            cn.Open();
            string dateoftheday = dateTimePicker1.Value.Year + "-" + dateTimePicker1.Value.Month + "-" + dateTimePicker1.Value.Day;
            SqlCommand cmd = new SqlCommand("insert into Clients (Imie,Nazwisko,Adres,Nr_rejstracji,Nr_telefonu,Marka_samochodu,Data,Rok,Moc_silnika,nr_VIN,Przebieg,Rzeczy_uszkodzone,Rzeczy_naprawione,Komentarz,Podsumowanie_ceny) values('" + textBox1.Text+"','"+ textBox63.Text + "','" + textBox3.Text + "','" + textBox2.Text + "','"+textBox5.Text+"','"+textBox4.Text+"','"+dateoftheday +"','"+textBox7.Text+"','"+textBox6.Text+"','"+textBox10.Text+"','"+textBox8.Text+"','"+textBox12.Text+"','"+textBox11.Text+"','"+textBox13.Text+ "','" + textBox62.Text + "')", cn); 
           int result= cmd.ExecuteNonQuery();
            if(result==1)
            {
                //second table
                SqlCommand cmd1 = new SqlCommand("select max(id) from Clients", cn);
                string refid = cmd1.ExecuteScalar().ToString();
                for(int i=0;i<dataGridView1.Rows.Count-1;i++)
                {
                    string itemname = dataGridView1[0, i].Value.ToString();
                    string qty = dataGridView1[1, i].Value.ToString();
                    string rate = dataGridView1[2, i].Value.ToString();
                    string totalprice = dataGridView1[3, i].Value.ToString();
                    SqlCommand cmdx = new SqlCommand("insert into Clients (Ref_ID,Nazwa_czesci,Ilosc,Cena,Cena_za_wszystko) values('" + refid+"','"+itemname+"','"+qty+"','"+rate+"','"+totalprice+"')", cn);
                    cmdx.ExecuteNonQuery();
                }

                MessageBox.Show("Profil zapisany!","Save",MessageBoxButtons.OK,MessageBoxIcon.Information);
                
                cn.Close();
            }

            
        }
        catch (Exception) { }
    }

Table has name 'dbo.Clients'

SELECT TOP (1000) [id]
  ,[Imie]
  ,[Nazwisko]
  ,[Adres]
  ,[Nr_rejstracji]
  ,[Nr_telefonu]
  ,[Marka_samochodu]
  ,[Data]
  ,[Rok]
  ,[Moc_silnika]
  ,[nr_VIN]
  ,[Przebieg]
  ,[Rzeczy_naprawione]
  ,[Podsumowanie_ceny]

FROM [MechanicProject].[dbo].[Clients]

Another table is 'Table'

SELECT TOP (1000) [id]
  ,[Imie]
  ,[Nazwisko]
  ,[Adres]
  ,[Nr_rejstracji]
  ,[Nr_telefonu]
  ,[Marka_samochodu]
  ,[Data]
  ,[Rok]
  ,[Moc_silnika]
  ,[nr_VIN]
  ,[Przebieg]
  ,[Rzeczy_naprawione]
  ,[Podsumowanie_ceny]

FROM [MechanicProject].[dbo].[Clients]

Does anybody know how to fix my issue? Data in the textBox should be stored in two tables. I have no idea why it doesn't work. Earlier, however, after replacing the computer, I lost the database. I guess I did everything as I did then. I have made new connections, etc. Thank you in advance for your help.



from Recent Questions - Stack Overflow https://ift.tt/2WT6eWv
https://ift.tt/eA8V8J

No comments:

Post a Comment