Hi
I need some help for updating my tabel, I'm using VS2008 winform with sql database.
It's really very basic but still I'm stucked here, I have some textboxes and buttons on a form and by clicking the add button a new record will be added to the database. I also have delete button and update button. Everything is working but not my update button. There is also a listbox that will be populated when a record is added or disappear when a record is deleted. I would like to choose from that listbox which one I should update.
I would like to stick to my approach as you can see in the code below.
This is for adding a new record!
MyDataAdapter = new SqlDataAdapter("SELECT * FROM NameTabel", myConnection);
MyCmd = new SqlCommandBuilder(MyDataAdapter);
MyDataSet = new DataSet();
MyDataAdapter.Fill(MyDataSet);
DataRow MyRow = MyDataSet.Tables[0].NewRow();
MyRow["FirstName"] = firstTextBox.Text;
MyRow["LastName"] = lastTextBox.Text;
MyRow["address"] = addressTextBox.Text;
MyRow["date"] = Convert.ToDateTime(dateTextBox.Text);
MyDataSet.Tables[0].Rows.Add(MyRow);
MyDataAdapter.Update(MyDataSet);This is for deleting a record choosen from my listbox1
MyDataAdapter = new SqlDataAdapter("SELECT * FROM NameTabel", myConnection);
MyCmd = new SqlCommandBuilder(MyDataAdapter);
MyDataSet = new DataSet();
MyDataAdapter.Fill(MyDataSet);
DataColumn[] MyKey = new DataColumn[1];
MyKey[0] = MyDataSet.Tables[0].Columns[0];
MyDataSet.Tables[0].PrimaryKey = MyKey;
DataRow FindMyRow = MyDataSet.Tables[0].Rows.Find(listBox1.SelectedValue);
FindMyRow.Delete();
MyDataAdapter.Update(MyDataSet);Can you please help me with this, thank you very much!

1 answers
Hi
I don't think I was lucky with my question, is there nobody who can help me?
If I was not clear enough, I try to explain again.
The code I show is the code for adding a new record from contents in my textboxes to my database, and the other code was for deleting a record. I have a listbox that displays all names I store in my database and by choosing a name in the listbox and push the delete button that record will be deleted.
The only thing I haven't managed yet is to be able to update a record by choosing the record from my listbox and then pushing the update button.
How can I do that?
answered one year ago by:
60
17279
Can you post the code you already have in the Update button Click eventhandler so that we can try to figure out why it's not working. I imagine you must also be handling the ListBox's SelectedIndexChanged event so that when the user selects a different record, your TextBoxes are populated with the existing entries for that record and the user can then edit them, if appropriate?
60
Hi Vulpes I solved this issue just a few minutes ago... I still don't get it, why could I use listbox1.selectedvalue in my delete statement but I have to use listbox1.selectedindex in my update statement?? Anyway, now it is working:) [code] int x = Convert.ToInt32(listBox1.SelectedIndex); MyDataAdapter = new SqlDataAdapter("SELECT * FROM NameTabel", myConnection); MyCmd = new SqlCommandBuilder(MyDataAdapter); MyDataSet = new DataSet(); MyDataAdapter.Fill(MyDataSet); MyDataSet.Tables[0].Rows[x][1] = firstTextBox.Text; MyDataSet.Tables[0].Rows[x][2] = lastTextBox.Text; MyDataSet.Tables[0].Rows[x][3] = addressTextBox.Text; MyDataSet.Tables[0].Rows[x][4] = Convert.ToDateTime(dateTextBox.Text); MyDataAdapter.Update(MyDataSet); [/code]
17279
If you'd have been using Rows.Find as you did for deletion, then I'd have expected listBox1.SelectedValue to find the row to update. However, if you're using Rows[], then it can only be indexed with an integer. Incidentally, no need to convert listBox1.Selectedindex to an integer as it already is one. However, it's best to always check that its greater than -1 (indicating there is in fact a selected item), otherwise you'll get an exception when you try to use it.