Dropdown box display issue in vb .net
-
Hello friends I have two radio buttons view and insert the view radio button display dropdown box, the dropdown box display the records retrieved from the database and displayed. (i) The first index is empty and the rest of the index is filled with the retrieved data i need the data to be displayed starting the first index. (ii) When i switch between the view and insert radio button and back to view radio button the data in the dropdown box is doubled for each of the above mentioned cycle Code: ----- Public Class Form1 Dim Conn As SqlConnection Dim Cmd As SqlCommand Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Conn = New SqlConnection Cmd = New SqlCommand Conn.ConnectionString = "Data Source=PERSONAL-A0CC22\SQLEXPRESS;Initial Catalog=personal;Integrated Security=True" End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If (StudentId.Text = "" Or Fname.Text = "" Or Lname.Text = "" Or Address.Text = "" Or Mobile.Text = "" Or EmailId.Text = "") Then Label1.Text = "Fill In All The Fields" Label1.Visible = True Else Conn.Open() Cmd = Conn.CreateCommand() Cmd.CommandText = "Insert into student values('" + StudentId.Text + "', '" + Fname.Text + "', '" + Lname.Text + "', '" + Address.Text + "', '" + Mobile.Text + "', '" + EmailId.Text + "')" Cmd.ExecuteNonQuery() StudentId.Text = "" Fname.Text = "" Lname.Text = "" Address.Text = "" Mobile.Text = "" EmailId.Text = "" Label1.Visible = True Label1.Text = "Data Have Been Stored Sucessfully" Conn.Close() End If End Sub Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged TStudentId.Visible = True StudentId.Visible = False Button1.Visible = False Button2.Visible = True Button3.Visible = True Dim i As Integer Dim da As New SqlDataAdapter Dim ds As New DataSet Dim dt As New DataTable Conn.Open() Cmd = Conn.CreateCommand Cmd.CommandText = "Select StudentId from Student" da.SelectCommand = Cmd da.Fill(ds, "Student") dt = ds.Tables("Stude
-
Hello friends I have two radio buttons view and insert the view radio button display dropdown box, the dropdown box display the records retrieved from the database and displayed. (i) The first index is empty and the rest of the index is filled with the retrieved data i need the data to be displayed starting the first index. (ii) When i switch between the view and insert radio button and back to view radio button the data in the dropdown box is doubled for each of the above mentioned cycle Code: ----- Public Class Form1 Dim Conn As SqlConnection Dim Cmd As SqlCommand Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Conn = New SqlConnection Cmd = New SqlCommand Conn.ConnectionString = "Data Source=PERSONAL-A0CC22\SQLEXPRESS;Initial Catalog=personal;Integrated Security=True" End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If (StudentId.Text = "" Or Fname.Text = "" Or Lname.Text = "" Or Address.Text = "" Or Mobile.Text = "" Or EmailId.Text = "") Then Label1.Text = "Fill In All The Fields" Label1.Visible = True Else Conn.Open() Cmd = Conn.CreateCommand() Cmd.CommandText = "Insert into student values('" + StudentId.Text + "', '" + Fname.Text + "', '" + Lname.Text + "', '" + Address.Text + "', '" + Mobile.Text + "', '" + EmailId.Text + "')" Cmd.ExecuteNonQuery() StudentId.Text = "" Fname.Text = "" Lname.Text = "" Address.Text = "" Mobile.Text = "" EmailId.Text = "" Label1.Visible = True Label1.Text = "Data Have Been Stored Sucessfully" Conn.Close() End If End Sub Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged TStudentId.Visible = True StudentId.Visible = False Button1.Visible = False Button2.Visible = True Button3.Visible = True Dim i As Integer Dim da As New SqlDataAdapter Dim ds As New DataSet Dim dt As New DataTable Conn.Open() Cmd = Conn.CreateCommand Cmd.CommandText = "Select StudentId from Student" da.SelectCommand = Cmd da.Fill(ds, "Student") dt = ds.Tables("Stude
I think the solution to your problem is quite simple, but I cannot be sure because your code snippet is so badly formatted that it is difficult to read. Next time you post some code please use the 'code block' widget (below the Text: box). Put the cursor on a new line, click the 'code block' and then paste your code between the opening and closing tags. That way all of your formatting etc. will be retained, making it easier for people to read and therefore more likely that you will get a response. OK, lecture over. To solve your problem, whilst using your existing code, modify your
RadioButton2_CheckedChanged
method like this:Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
TStudentId.Visible = True
StudentId.Visible = False
Button1.Visible = False
Button2.Visible = True
Button3.Visible = TrueDim i As Integer
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim dt As New DataTableTStudentId.Items.Clear() '<========================= HERE'S the change, get rid of the old stuff before adding the new
Conn.Open()
Cmd = Conn.CreateCommand
Cmd.CommandText = "Select StudentId from Student"
da.SelectCommand = Cmd
da.Fill(ds, "Student")
dt = ds.Tables("Student")
For i = 0 To dt.Rows.Count - 1
TStudentId.Items.Add(dt.Rows(i).Item(0))
NextFname.Text = ""
Lname.Text = ""
Address.Text = ""
Mobile.Text = ""
EmailId.Text = ""
Conn.Close()
End SubBut I have to ask. Why not just set TStudentId.DataSource to dt, rather than use the For..Next loop as you do? Another lecture coming up, sorry! :) For your own peace of mind, do try to stop using silly member names (
da
for a DataAdapter,dt
for a DataTable), sure you remember what they do now, but in six months you won't. And what are you going to do when you need more than one DataAdapter, useda2
? Instead of dt, how about StudentIdTable, or tblStudentID, and so on. Also RadioButton2, what does it do? Again you know now, but in six months?? And more importantly in this case It took me a lot more reading to work out what was going on than it should have done. Give all members and controls (with the possible exception of Labels) sensible descriptive names. How aboutrbtnShowId
for example 'rbtn' tells me it is a RadioButton 'ShowId' tells me that it shows/hides the Id Dropdown, devise your own naming scheme and stick t