Click here to Skip to main content
15,921,694 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Evryone,

I'm new on this section.

I have problem about my code. I tried everything I can but I cannot solve the problem.

The problem is, in my code SELECT, DELETE, INSERT queries working fine. But UPDATE statement doesn't work. And there was no error. Evert parameters goes to correct location everything is okay but it's not updating my data.

P.S: I'm using MS Access.

My Code:

The Button:
C#
private void btn_duzenle_Click(object sender, EventArgs e)
{
    if (txt_kurumadi.Text != "")
    {
        string komut = "UPDATE kurum_listesi SET kurum_adi=@kurum_adi, kurum_tipi=@kurum_tipi, kurum_il=@kurum_il, kurum_icmaldurumu=@kurum_icmaldurumu WHERE kurum_id=@kurum_id";
        string deger1, deger2, deger3, deger4;
        bool deger5 = false;
        string parametre1="@kurum_id", parametre2 = "@kurum_adi", parametre3 = "@kurum_tipi", parametre4 = "@kurum_il", parametre5 = "@kurum_icmaldurumu";
        deger1 = dgv_tablo.CurrentRow.Cells[0].Value.ToString();
        deger2 = txt_kurumadi.Text;
        deger3 = cb_kurumtipi.Text;
        deger4 = cb_il.Text;
        if (chb_icmaldurumu.Checked == true)
        {
            deger5 = true;
        }
        else
        {
            deger5 = false;
        }
        p_vt.P_BesParametre(komut,parametre1,parametre2,parametre3,parametre4,parametre5,deger1,deger2,deger3,deger4,deger5);
        p_dgvduzenle();
    }
}


And the other code:
C#
public void P_BesParametre(string komut, string parametre1, string parametre2, string parametre3, string parametre4, string parametre5,string parametredeger1, string parametredeger2, string parametredeger3, string parametredeger4, bool parametredeger5)
{
    try
    {
        p_baglanti.Open();
        OleDbCommand p_komut = new OleDbCommand(komut, p_baglanti);
        p_komut.Parameters.AddWithValue(parametre1, parametredeger1);
        p_komut.Parameters.AddWithValue(parametre2, parametredeger2);
        p_komut.Parameters.AddWithValue(parametre3, parametredeger3);
        p_komut.Parameters.AddWithValue(parametre4, parametredeger4);
        p_komut.Parameters.AddWithValue(parametre5, parametredeger5);
        p_komut.ExecuteNonQuery();
        p_komut.Parameters.Clear();
        p_baglanti.Close();
    }
    catch (Exception hata)
    {
        System.Windows.Forms.MessageBox.Show("Kritik Hata! \n" + hata);
        throw;
    }
}


Sorry for the inconvience.

-----------------------------------------------------------------------------------------------------------
Hello Everyone.

This is not an answer but I change my database to MSSQL Express and I also change my code. And problem solved...

And I never solve this mystery.

Thank you everyone!
Posted
Updated 23-Dec-15 0:33am
v3
Comments
Leo Chapiro 12-Nov-15 7:35am    
IMAGE OF CODE is not good as we can't work with it (copy/paste/change) , please add the code itself!
OriginalGriff 12-Nov-15 8:06am    
And the image is completely unreadable...
Andy Lanng 12-Nov-15 8:18am    
(you can enlarge it :))
OriginalGriff 12-Nov-15 9:56am    
(If I could be bothered, I would - but if he can't be bothered to even make it slightly easy for us, I can't be bothered to to do that! :laugh: )
YcD44 12-Nov-15 8:41am    
I updated and added code.
Sorry for the inconvience.

1 solution

a few note (too long for a comment)

to test:
before the update
1: select the row by id - check it exists
2: check the values against the new values to see if they actually need to change

during the update:

C#
int rowsAffected = p_komut.ExecuteNonQuery();

if rowsAffected > 0 then something was affected. Zero would mean that no rows where found

after the update
select the row again and perform the previous "before the update" steps.



Other notes:

you don't need to ever say if(x == true). it's redundant. if(x) will do so the line in your if should read if(chb_icmaldurumu.Checked)

You don't need tp pass through param1, param2 etc. you can use a param type:

C#
public void P_BesParametre(string komut, param KeyValuePair[] parametreVeDeger){
    
  OleCommand p_kamut = new OleDbCommand(komut, p_baglanti);

  foreach(var pvd in parametreVeDeger){
    p_komut.Parameters.AddWithValue(pvd.Key, pvd.Value);
  }

  try{ 

    p_baglanti.Open();
    p_komut.ExecuteNonQuery
    p_baglanti.Close();

  }catch{}

}


usage:
C#
P_BesParametre(komut, new KeyValuePair{Key="@p1",Value="value"},new KeyValuePair{Key="@p2",Value="value"},new KeyValuePair{Key="@p3",Value="value"});
or
List<keyvaluepair> kvps = new ...

kvps.Add(new KeyValuePair{Key="@p1",Value="value"});
kvps.Add(new KeyValuePair{Key="@p2",Value="value"});
kvps.Add(new KeyValuePair{Key="@p3",Value="value"});

P_BesParametre(komut, kvps.ToArray())</keyvaluepair>
 
Share this answer
 
Comments
YcD44 12-Nov-15 9:20am    
Thank for your answer Andy Laang.
First of all Thank you for your notes. I done them all.

I did the rowsaffected and as you say it returns zero. So there was no update.

After that I tried to use your code suggestion but on "params KeyValuePair[] parametreVeDeger" code KayValuePair doesn't become var,var its only becomes string, string or string, int.

How can i do for that? May I change my table's datatypes or there is a other way?

Sorry for the lots of questions.
Andy Lanng 12-Nov-15 9:23am    
TBH that was the short answer. You should add parameters with the name and type and add a value after, but that's not the main issue. The issue is that your update does not find a row to update.

Try changing it to a select. You can keep most of the other code but you'll just need 1 parameter for kurum_id. Does that return any results?
YcD44 12-Nov-15 9:41am    
Select with parameter working fine.
Andy Lanng 12-Nov-15 9:43am    
hmm - that's,... unexpected.

There must be something else blocking the transaction :S

What type of db are you using?
YcD44 12-Nov-15 10:04am    
MS Access. .accdb

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900