Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
salamo 3alaykoum
i'm a biginner of C# and i want to execute a button Update, that it suceptible to update any Line Of DataGridView, but when i put the button some errors is showed
as ==> int a = Convert.ToInt32(selectedRow.Cells[0].Value);
please if anyone had a solution text me
private void button3_Click(object sender, EventArgs e){
           int selectedrowindex = admuserDataGridView.SelectedCells[0].RowIndex;
           DataGridViewRow selectedRow = admuserDataGridView.Rows[selectedrowindex];
           //int a = Convert.ToInt32(selectedRow.Cells["ItemId"].Value);
           int a = Convert.ToInt32(selectedRow.Cells[0].Value);
           try{cmd.Connection = cnx;
               cmd.CommandType = CommandType.Text;
               cnx.Open();
cmd.CommandText = "update admuser set Designation = '" + textdes.Text + "', Quantity = '" + txtQ.Text + "', PR = '" + textpr.Text + "', PO ='" + textpo.Text + "',Main Asset = '" + textMain.Text + "',Sec Asset = '" + textSec.Text + "',Serial Number = '" + textsn.Text + "',Process = '" + textp.Text + "',Supplier = '" + textSp.Text + "',Status = '" + textSt.Text + "',Original Project = '" + textorp.Text + "',Current Project = '" + textcup.Text + "', purchasing date = '" + timer.Value.Date.ToString() + "', Price = '" + textprice.Text + "',Dep Period = '" + textDep.Text + "',NBV = '" + textNBV.Text + "',State = '" + textState.Text +  "' where Item=" + a ;
               cmd.ExecuteNonQuery();
               cnx.Close();
               MessageBox.Show("U :D ", "PS");
               this.Close();
               Administrator ad = new Administrator();
               ad.Show();}
           catch(Exception ex){MessageBox.Show(ex.ToString()); } } }


What I have tried:

i'm a biginner of C# and i want to execute a button Update, that it suceptible to update any Line Of DataGridView
Posted
Comments
[no name] 13-Apr-17 8:20am    
The most likely thing is that whatever is in selectedRow.Cells[0] can't be converted to an int. But since you made the decision not to tell us what the error is, that is just a guess. What I don't need to guess at though, is you inviting people to wipe out your database through an SQL inject attack.
Si Mohammed 13-Apr-17 8:47am    
It's juste an excercice bro like a test,
So i don't care about SQL inject attack
However
Why the SelectedRow.Cell[0] can't be converted to an int
[no name] 13-Apr-17 9:11am    
"It's juste an excercice bro like a test", so what? Do it right the first time and then you won't have to do it over and over and over again.
"Why the SelectedRow.Cell[0] can't be converted to an int", well, off hand I would say that it's because whatever is in SelectedRow.Cell[0] isn't a string that can be converted to an int. Just like I ALREADY told you.
Si Mohammed 13-Apr-17 9:49am    
Thank you about your advice

ya i anderstaud,
thanks again
Dave Kreskowiak 13-Apr-17 9:16am    
You better care about it because what you're doing in this code is reinforcing really bad habits and making it far harder to diagnose problems in your code. Rewrite to use parameterized queries instead and you might find that your errors go away.

But, since you didn't think the error messages would be important (THEY ALWAYS ARE!) there's really nothing else solid we can tell you.

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