1

My app shows a menu of a restaurant where, after logging in with an email and password from the database, the client guides you to a computer that calculates Kcal after certain dates. I would like to be able to put the result in the same database and the same data that the client used as the image. This is the calculator [This is the database for login and where i wanna put the result

This is for an application I have for a contest

private void button1_Click(object sender, EventArgs e)
{
    ani = Convert.ToInt32(textBox1.Text);
    cm = Convert.ToInt32(textBox2.Text);
    kg = Convert.ToInt32(textBox3.Text);
    s = ani + cm + kg;
    if (s < 250) label_mesaj.Text = "1800";
    else if (s >= 250 && s <= 275) label_mesaj.Text = "2200";
    else label_mesaj.Text = "2500";
}

private void button2_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(constr);
    con.Open();
    SqlCommand cmd = new SqlCommand("insert into Clienti(parola,nume,prenume,adresa,email)values('" + textBox4.Text + "','" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox6.Text + "')", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
}

This is code that add to my database new clients after a registration, so i don't know how to remember that "id_clienti" I expect that "label_mesaj" to show on the "kcal_zilnice" in the database after LogIn with the same data.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
Sweet
  • 11
  • 3
  • 2
    Can you please add your code that inserts into the Azure database? A [mcve] would be great. – Jeremy Thompson Apr 01 '19 at 19:58
  • Also be careful with `con.Open()`. I don't see the corresponding `con.Close()`. You may want to consider `using (SqlConnection con = new SqlConnection(constr)){...}` instead as this will close and dispose the connection for you. – Jaskier Apr 01 '19 at 20:10
  • Its not about that con.Close() because i want to remember a id when i login, after login and calculating the "kcal" i want to add that answer to that id in database. – Sweet Apr 01 '19 at 20:15
  • since you are using numbers as ids i assume you have an id that keeps increasing so once data is added to your database you could try fetching the last id which means that the following article could be useful https://social.msdn.microsoft.com/Forums/vstudio/en-US/be27ce82-d8ce-4360-b86e-4859811f442a/how-to-get-the-last-inserted-primary-key-value?forum=csharpgeneral, and then store it as a value of a field, also check this answer https://stackoverflow.com/questions/18373461/execute-insert-command-and-return-inserted-id-in-sql – mdln97 Apr 01 '19 at 20:22
  • I will try, but to be clear, after the customer logs in "Form1" it enters Form2 where the calculator is, I want to remember which client has logged in "Form1" to add the result from "Form2" in the database for that client. – Sweet Apr 01 '19 at 20:31
  • 1
    just pass in the constructor of the second form the id of that customer, or you could try implementing a singleton pattern in a class that will keep all the customer details stored during the "logged in" session – mdln97 Apr 01 '19 at 20:34
  • Ah ok, now i understant, i will try, thank you – Sweet Apr 01 '19 at 20:47
  • 2
    @Sweet Your code is susceptible to SQL injection attacks - instead of building SQL statements as strings, you should use named parameters so that the values get escaped correctly. You should never build SQL statements from user input. – xxbbcc Apr 01 '19 at 21:16
  • I just solve the requirements imposed by a contest and so I am asked to resolve – Sweet Apr 01 '19 at 21:43
  • We don't put solved in the titles. Just accept the answer that you posted. – LarsTech Apr 02 '19 at 14:27

1 Answers1

0

That's how I managed to solve the problem The LogIn part that I remember the email

private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(constr);
        SqlDataAdapter da = new SqlDataAdapter("Select count(*) from Clienti where email='" + textBox1.Text + "' and Parola='" + textBox2.Text + "'", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        if (dt.Rows[0][0].ToString() == "1")
        {
            client.email = textBox1.Text;
            this.Hide();
            Form4 ssss = new Form4();
            ssss.Show();
        }
        else MessageBox.Show("verifica datele");
    }

And in the next Form

private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(constr);
        SqlDataAdapter da = new SqlDataAdapter();
        con.Open();if (s < 250)  label_mesaj.Text = "1800";
        da.UpdateCommand = new SqlCommand("Update Clienti set kcal_zilnice= '" + label_mesaj.Text + "' where email= '" + Form3.client.email.ToString() + "'", con);
        da.UpdateCommand.ExecuteNonQuery();
        da.UpdateCommand.Dispose();
        con.Close();
    }
Sweet
  • 11
  • 3