Yunus Emre Altanay
Yunus Emre Altanay

Reputation: 5

Update command not working but there is no Error

I have a table named MesaiBilgileri and columns Id, AdiSoyadi, Mesai, MesaiTarih...

I'd pick the info from table named PersonelBilgileri with this code:

private void cboxAdSoyadMesai_SelectedIndexChanged(object sender, EventArgs e)
{
    string secilipersonel = cboxAdSoyadMesai.Text;
    baglan.Open();

    SqlCommand komut = new SqlCommand("Select *from PersonelBilgileri WHERE AdiSoyadi = '" + secilipersonel + "'", baglan);

    SqlDataReader oku = komut.ExecuteReader();

    while (oku.Read())
    {
        lbId.Text = oku["Id"].ToString();
    }

    baglan.Close();
}

I'd use invisible label to take items Id for write another table...

Then with this values I'd go to another table and update that table with this code:

private void mesaiekle()
{
    string secilitarih = dtpMesai.Value.ToShortDateString(); //combobox for pick date
    string secilipersonel = cboxAdSoyadMesai.Text; //combobox for pick employee
    int mesaiId;
    mesaiId = Convert.ToInt32(lbId.Text); //another table's Id value from invisible label...

    baglan.Open();

    SqlCommand komut = new SqlCommand("UPDATE MesaiBilgileri SET Id = '"+ mesaiId +"' , AdiSoyadi = '"+ secilipersonel +"' ,  Mesai = '" + cboxSaatMesai.Text + "' , MesaiTarih = @tarih", baglan);
    komut.Parameters.Add("@tarih", SqlDbType.DateTime).Value = secilitarih;

    komut.ExecuteNonQuery();

    baglan.Close();
}

With this code when I push the save button;

private void btnMesaiEkle_Click(object sender, EventArgs e)
{
    if ((cboxSaatMesai.Text == string.Empty) && (cboxAdSoyadMesai.Text == string.Empty))
    {
        MessageBox.Show("Eklemek İstediğiniz Mesai Bilgilerini Seçmek Zorundasınız", "HATA!!"); 
    }
    else
    {
        mesaiekle();
        MessageBox.Show("" + cboxAdSoyadMesai.Text + " İçin Mesai Eklendi", "Mesai Ekleme Durumu");
    }
}

When I push the save button I see the confirmation messagebox but table is not updated... There is no error... I'm new at coding so what can I do?

Sorry for my English :/

Upvotes: 0

Views: 195

Answers (2)

sujith karivelil
sujith karivelil

Reputation: 29016

You have to note several things here. You are dealing with different datatypes in this single query, The variable mesaiId is defined as integer(not sure how the field is defined in the database). you need not to pass a integer value within a pair of single quotes. And why some values are parameters? why not the whole query params? Make a try with this:

SqlCommand komut = new SqlCommand("UPDATE MesaiBilgileri SET Id = @mesaiId, AdiSoyadi =@secilipersonel,Mesai = @cboxSaatMesai, MesaiTarih = @tarih", baglan);

komut.Parameters.Add("@mesaiId", SqlDbType.Int).Value = mesaiId;
komut.Parameters.Add("@secilipersonel", SqlDbType.VarChar).Value = secilipersonel;
komut.Parameters.Add("@cboxSaatMesai", SqlDbType.VarChar).Value = cboxSaatMesai.Text;
komut.Parameters.Add("@tarih", SqlDbType.DateTime).Value = secilitarih;

komut.ExecuteNonQuery();

Note :- Keep in mind that it will update all rows in the table since you have not specified any condition, if you need to update specific rows then you have to add Where Id = @mesaiId or something like that

Upvotes: 1

McNets
McNets

Reputation: 10807

string secilitarih = dtpMesai.Value.ToShortDateString();

Build your update command without use parameters:

SqlCommand komut = new SqlCommand("UPDATE MesaiBilgileri SET Id = '"
                                  + mesaiId 
                                  +"' , AdiSoyadi = '"
                                  + secilipersonel 
                                  +"' ,  Mesai = '" 
                                  + cboxSaatMesai.Text 
                                  + "' , MesaiTarih = " 
                                  + "cast ('" + secilitarih  + "' as datetime) "
                                  + ", baglan);

Add a message box or a breakpoint to check the command text.

MessageBox.Show(komut.CommandText);

If you have some problem with date format change secilitarih to:

string secilitarih = dtpMesai.Value.ToString("yyyy-MM-dd");

Upvotes: 0

Related Questions