Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Dropdown box display issue in vb .net

Dropdown box display issue in vb .net

Scheduled Pinned Locked Moved Visual Basic
databasecsharpsecurityjsonhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 4510968
    wrote on last edited by
    #1

    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

    H 1 Reply Last reply
    0
    • U User 4510968

      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

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      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 = True

      Dim i As Integer
      Dim da As New SqlDataAdapter
      Dim ds As New DataSet
      Dim dt As New DataTable

      TStudentId.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))
      Next

      Fname.Text = ""
      Lname.Text = ""
      Address.Text = ""
      Mobile.Text = ""
      EmailId.Text = ""
      Conn.Close()
      End Sub

      But 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, use da2? 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 about rbtnShowId 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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups