where is your
param variable defined? For that matter, where are you declaring the
con,
cmd,
da, and
ds objects?
I was able to duplicate the problem you are seeing. I believe it is related to the fact that your DataSet object doesn't go out of scope when the button_click event ends. SqlParameters and DataSets are a little wierd. They don't like being reused. Once you add the paramter as "Joe Customer" the first time, it works but it remembers those same values the second time you use it with a different customer name: "Jane Customer". It still has all the values from the first customer
as well as the values from the second customer, but the first customer record is the one that is in the list first. Since the text boxes always populate from the first row of the dataset, the second customer information is never displayed.
Also, I noticed you are setting the value of
param, but you never use it. Instead the parameter value is being added to the command with the line:
- cmd.Parameters.AddWithValue("@uname", TextBox2.Text)
So you really don't need the
param variable at all.
I was able to get this to work splendidly once I declared the
ds object locally. (Or you could set
ds to a New instance of a DataSet. Either way works.)
Here is the code after I added a line to reset the
ds object to a new instance every time:
- Try
-
con.Open()
-
'param.ParameterName = "@uname" NOT USED
-
'param.Value = TextBox2.Text NOT USED
-
cmd = New SqlCommand("select * from customer where custname=@uname", con)
-
cmd.Parameters.AddWithValue("@uname", TextBox2.Text)
-
da = New SqlDataAdapter(cmd)
-
ds = New DataSet 'Dataset must be reset by setting it to a new instance
-
da.Fill(ds)
-
TextBox1.Text = ds.Tables(0).Rows(0).Item(0)
-
TextBox2.Text = ds.Tables(0).Rows(0).Item(1)
-
TextBox4.Text = ds.Tables(0).Rows(0).Item(3)
-
TextBox5.Text = ds.Tables(0).Rows(0).Item(5)
-
DateTimePicker1.Value = ds.Tables(0).Rows(0).Item(4)
-
TextBox3.Text = ds.Tables(0).Rows(0).Item(2)
-
-
Catch ex As Exception
-
MsgBox(ex.Message.ToString)
-
End Try
-
con.Close()