I have a form for my update method, the form is in detail view. Next to the textboxes I have a listbox which shows the names of all the names in the database table. Under the listbox I also have an extra textbox to quick search the name in case user would like to type it in.
When I go to update one of the names, such as changing John to Jonathan, the database updates with the new name as I have checked on sql server, but the name in the listbox does not change! There’s a dirty way to fix this by moving the position the listbox is selected on currently to movefirst(). However, under the listbox I have the textbox which is a quick search as I’ve mentioned, so I go to type Jonathan in the search text box, but nothing appears. However, if I type the former name John, then I get the details of this row in the table.
Is there a way I can fix this?
UPDATE 1:
Ive tried making the listbox datasource null then reassigning it again but it doesen’t work. Ive put my code for my update form below.
namespace WindowsFormsApplication1
{
public partial class updateContact : Form
{
public updateContact()
{
InitializeComponent();
}
private void updateContact_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'tblcontactsupdate.tblContacts' table. You can move, or remove it, as needed.
this.tblContactsTableAdapter.Fill(this.tblcontactsupdate.tblContacts);
}
private void btnUpdateContact_Click(object sender, EventArgs e)
{
int x;
Program.da.UpdateCommand = new SqlCommand("Update tblContacts SET FIRSTNAME = @FIRSTNAME, LASTNME = @LASTNME WHERE ID = @ID", Program.cs);
Program.da.UpdateCommand.Parameters.Add("@FIRSTNAME", SqlDbType.VarChar).Value = fIRSTNAMETextBox.Text;
Program.da.UpdateCommand.Parameters.Add("@LASTNME", SqlDbType.VarChar).Value = lASTNMETextBox.Text;
Program.da.UpdateCommand.Parameters.Add("@ID", SqlDbType.VarChar).Value = iDTextBox.Text;
Program.cs.Open();
x = Program.da.UpdateCommand.ExecuteNonQuery();
Program.cs.Close();
if (x >= 1)
{
MessageBox.Show("Record(s) has been updated");
Program.ds.Clear();
Program.da.Fill(Program.ds);
txtfindUpdatecontact.Text = "";
//lbupdateContact.DataSource = null;
//lbupdateContact.DataSource = this.tblcontactsupdate.tblContacts;
}
}
private void txtfindUpdatecontact_TextChanged(object sender, EventArgs e)
{
if (!txtfindUpdatecontact.Text.Equals(""))
{
this.tblContactsBindingSource.Filter = "FIRSTNAME = '" + txtfindUpdatecontact.Text + "'";
}
else
{
this.tblContactsBindingSource.RemoveFilter();
}
}
private void lbupdateContact_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void iDTextBox_TextChanged(object sender, EventArgs e)
{
}
private void fIRSTNAMETextBox_TextChanged(object sender, EventArgs e)
{
}
private void lASTNMETextBox_TextChanged(object sender, EventArgs e)
{
}
}
}
You will have to set the
DataSourceof your listbox one more time after updating the source.Something like below: It’s my data:
then in the form’s constructor you can do:
then for updation, something like below:
This is just an example change code according to your need.