Question Data Source causing Problems!!!

gautamshaw

New member
Joined
Aug 15, 2009
Messages
4
Programming Experience
Beginner
I have two buttons:Show and Refresh to deal with.
Here lies my code for the show button:
VB.NET:
Private Sub Show_Btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Show_Btn.Click
        con = New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\Users\Gautam\Documents\Visual Studio 2005\Projects\Contacts\Contacts\Contacts.mdb")
        con.Open()
        cmd = New OleDbCommand("Select * from MContacts where MobileNo=@mobileno", con)
        cmd.Parameters.AddWithValue("@mobileno", ComboBox1.Text)
        Dim da As New OleDbDataAdapter(cmd)
        Dim dt As New DataTable
        da.Fill(dt)
        con.Close()
        Dim bs1 As New BindingSource()
        bs1.DataSource = dt
        ComboBox1.DataSource = bs1
        ComboBox1.DisplayMember = "MobileNo"
        Dim bs2 As New BindingSource()
        bs2.DataSource = dt
        ComboBox2.DataSource = bs2
        ComboBox2.DisplayMember = "Salutation"
        Dim bs3 As New BindingSource()
        bs3.DataSource = dt
        ComboBox3.DataSource = bs3
        ComboBox3.DisplayMember = "ContactName"
        Dim bs4 As New BindingSource()
        bs4.DataSource = dt
        ComboBox4.DataSource = bs4
        ComboBox4.DisplayMember = "Catagory"
        Dim bs5 As New BindingSource()
        bs5.DataSource = dt
        ComboBox5.DataSource = bs5
        ComboBox5.DisplayMember = "SubCatagory"
        TextBox1.DataBindings.Clear()
        TextBox1.DataBindings.Add("text", dt, "CompanyName")
        TextBox2.DataBindings.Clear()
        TextBox2.DataBindings.Add("text", dt, "Designation")
        TextBox3.DataBindings.Clear()
        TextBox3.DataBindings.Add("text", dt, "Email")
        TextBox4.DataBindings.Clear()
        TextBox4.DataBindings.Add("text", dt, "PhNo")
        TextBox5.DataBindings.Clear()
        TextBox5.DataBindings.Add("text", dt, "MessangerId")
        TextBox6.DataBindings.Clear()
        TextBox6.DataBindings.Add("text", dt, "HAddress")
        TextBox7.DataBindings.Clear()
        TextBox7.DataBindings.Add("text", dt, "OAddress")
        DateTimePicker1.DataBindings.Clear()
        DateTimePicker1.DataBindings.Add("text", dt, "Birthday")
        DateTimePicker2.DataBindings.Clear()
        DateTimePicker2.DataBindings.Add("text", dt, "Anniversary")
        TextBox8.DataBindings.Clear()
        TextBox8.DataBindings.Add("text", dt, "Notes")
    End Sub
This works fine.

Here lies my code for the refresh button:
VB.NET:
Private Sub Refresh_Btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Refresh_Btn.Click
ComboBox2.SelectedIndex = -1
 con = New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\Users\Gautam\Documents\Visual Studio 2005\Projects\Contacts\Contacts\Contacts.mdb")
        con.Open()
        cmd = New OleDbCommand("Select * from MContacts", con)
        Dim da As New OleDbDataAdapter(cmd)
        Dim dt As New DataTable
        da.Fill(dt)
        con.Close()
        Dim bs1 As New BindingSource()
        bs1.DataSource = dt
        ComboBox1.DataSource = bs1
        ComboBox1.DisplayMember = "MobileNo"
        ComboBox1.SelectedIndex = -1
        Dim bs2 As New BindingSource()
        bs2.DataSource = dt
        ComboBox3.DataSource = bs2
        ComboBox3.DisplayMember = "ContactName"
        ComboBox3.SelectedIndex = -1
        Dim bs3 As New BindingSource()
        bs3.DataSource = dt
        ComboBox4.DataSource = bs3
        ComboBox4.DisplayMember = "Catagory"
        ComboBox4.SelectedIndex = -1
        Dim bs4 As New BindingSource()
        bs4.DataSource = dt
        ComboBox5.DataSource = bs4
        ComboBox5.DisplayMember = "SubCatagory"
        ComboBox5.SelectedIndex = -1
        DateTimePicker1.Value = Date.Today
        DateTimePicker2.Value = Date.Today
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox6.Text = ""
        TextBox7.Text = ""
        TextBox8.Text = ""
    End Sub

When i show a data using the show button click and then click the refresh button to clear the fields then the combobox2 is causing the problem.
On clicking the refresh button,the combobox2 dropdownlist items diappears.....
So what if the user wants to save a data to the database at that time using the save button?
The user cant select an item from the combobox2 dropdownlist then......
how to solve this?
 
I'd start by deleting the code you'd written, then reading the DW2 link in my signature, section Creating a Simple Data App.
 
Back
Top