mardi 8 décembre 2015

Data rows are being added with no data in my table, need a if statement to stop this?

I am currently doing some work on a contact formular, I can make the text from my textboxes go into my coloumns, but if I dont type anything I dont want it to make a new row in the tabel with no data, which it is doing right now.

So I am guessing I need a if statement, but I dont know the syntax to make it.

This is my code:

    string Navn = TextBoxNavn.Text;
    string Emne = TextBoxEmne.Text;
    string Email = TextBoxEmne.Text;
    string Besked = TextBoxBesked.Text;

    SqlConnection conn = new SqlConnection

    (ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());

    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;

    cmd.CommandText = "INSERT INTO Kontakt (Navn, Emne, Email, Besked) VALUES (@Navn, @Emne, @Email, @Besked)";
    cmd.Parameters.AddWithValue("@Navn", Navn);
    cmd.Parameters.AddWithValue("@Emne", Emne);
    cmd.Parameters.AddWithValue("@Email", Email);
    cmd.Parameters.AddWithValue("@Besked", Besked);

    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();

Aucun commentaire:

Enregistrer un commentaire