c# - update DB with datagridview enter event -


  1. i want update database when click on cell of datagridview in fact want edit database , datagridview clicking on cell.

  2. and have id in database it's auto fill , when insert , save s.th need id fill sql.and there codes not work :

code 1.

     private void dgvlist_enter(object sender, eventargs e)    {        sqlconnection con = new sqlconnection();        sqlcommand cmd = new sqlcommand();        sqldataadapter da = new sqldataadapter();        con.connectionstring = "server=(local);database=tel;trusted_connection=yes;";        con.open();        cmd.connection = con;        da.selectcommand = cmd;        datagridviewcell cell = dgvlist.selectedcells[0] datagridviewcell;        string value = cell.value.tostring();        cmd.commandtext = ("update information set name =@name , family=@family , telephone=@telephone , mobile = @mobile id = @id");        cmd.parameters.addwithvalue("@id", value);        cmd.parameters.addwithvalue("@name", value);        cmd.parameters.addwithvalue("@family", value);        cmd.parameters.addwithvalue("@telephone", value);        cmd.parameters.addwithvalue("@mobile",  value);        cmd.executenonquery();    } 

code 2.

    private void btnsave_click(object sender, eventargs e)     {         if (txtname.text == null || txtfamily.text == null || txttel.text == null)         {             messagebox.show("please fill neccessary boxes");         }         else         {             save();         }     }     void save()     {         sqlconnection con = new sqlconnection();         sqlcommand cmd = new sqlcommand();         sqldataadapter da = new sqldataadapter();         con.connectionstring = "server=(local);database=tel;trusted_connection=yes;";         con.open();         cmd.connection = con;         da.selectcommand = cmd;         cmd.commandtext = ("insert information id = @id , name =@name , family=@family , telephone=@telephone , mobile = @mobile id = @id");         cmd.parameters.addwithvalue("@id", "");         cmd.parameters.addwithvalue("@name", txtname.text);         cmd.parameters.addwithvalue("@family", txtfamily.text);         cmd.parameters.addwithvalue("@telephone", txttel.text);         cmd.parameters.addwithvalue("@mobile", txtmobile.text);         cmd.executenonquery();     } 

simple answer

  1. datagridview.enter triggered when datagridview receives focus , entered; not when enter new cell. also, changes haven't been madeuntil after you've entered cells. lastly, update fails because where id = @id isn't evaluating true when think should.
  2. your sql insert statement incorrectly formatted, error states: "incorrect syntax".

deeper explanation

let's assume have following database setup:

+---+-------+--------+-----------+----------+ |id | name  | family | telephone | mobile   | +---+-------+--------+-----------+----------+ |1  | john  | smith  | 123-4567  | 123-4560 | |2  | jane  | doe    | 987-6543  | 987-6540 | |3  | jenny | tutone | 867-5309  | 867-5309 | +---+-------+--------+-----------+----------+ 

and resulting datagridview:

datagridview image containing equivalent data mocked database


now suppose misspelled john smith's name, should jon instead. assuming other control has focus @ time, click on cell in row 0 column 1 - method triggered (before changes made):

    datagridviewcell cell = dgvlist.selectedcells[0] datagridviewcell;     string value = cell.value.tostring();     cmd.commandtext = ("update information set name =@name , family=@family , telephone=@telephone , mobile = @mobile id = @id");     cmd.parameters.addwithvalue("@id", value);     cmd.parameters.addwithvalue("@name", value);     cmd.parameters.addwithvalue("@family", value);     cmd.parameters.addwithvalue("@telephone", value);     cmd.parameters.addwithvalue("@mobile",  value); 

effectively, query being run is:

update information set name ='john' , family='john' , telephone='john' , mobile = 'john' id = 'john' 

since there no records where id = 'john', no records updated. on other hand, had instead first clicked on cell in row 0 column 0, query have looked liked:

update information set name ='1' , family='1' , telephone='1' , mobile = '1' id = '1' 

which result in following database changes:

+---+-------+--------+-----------+----------+ |id | name  | family | telephone | mobile   | +---+-------+--------+-----------+----------+ |1  | 1     | 1      | 1         | 1        | |2  | jane  | doe    | 987-6543  | 987-6540 | |3  | jenny | tutone | 867-5309  | 867-5309 | +---+-------+--------+-----------+----------+ 

therefore, need fix values set in command parameters. should consider moving updating code different method, such datagridview.cellendedit, it's updated after changes made in current cell.


as insert statement, simple google search have shown correct format. change following:

cmd.commandtext = ("insert information id = @id , name =@name , family=@family , telephone=@telephone , mobile = @mobile id = @id"); 

to following:

cmd.commandtext = ("insert information (name, family, telephone, mobile) values (@name, @family, @telephone, @mobile)"); 

there should no need add id statement should identity column in database , auto-generated.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -