Need some Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • icetalks
    New Member
    • Feb 2007
    • 8

    Need some Help

    hi,
    I am building a form that gets its data base from SQL data base . i have about 3 combo Boxes in this form that shows some data memebers . ok now the question ... if the user selects a value from any of these combo boxes i want to get the ID of that Value .. i mean : i have a course table contains course_id and the Course_name when the user selects the Course_Name from the combo box i want to retreive the ID of that course in to a new table named School_Course. ....

    please any ideas . with sample code if possible .... plz.


    Thank you all ... very much ;)
  • Lawrence 007
    New Member
    • Nov 2006
    • 41

    #2
    That depend on what version of Visual Basic you are doing this in.
    Visual studio you can set the Value member and the display member in the properties screen of the combo box. If you have visual basic express (the new free version from Microsoft) it is extremely easy to set up you display and value members.

    Let me know what version you have and I can see what I can do.

    Comment

    • icetalks
      New Member
      • Feb 2007
      • 8

      #3
      Originally posted by Lawrence 007
      That depend on what version of Visual Basic you are doing this in.
      Visual studio you can set the Value member and the display member in the properties screen of the combo box. If you have visual basic express (the new free version from Microsoft) it is extremely easy to set up you display and value members.

      Let me know what version you have and I can see what I can do.


      .. am using VB.NET .... 2003 .... Coonection mode. not wizard. .. SQL Server2000

      Comment

      • ashasprabhu
        New Member
        • Jan 2007
        • 52

        #4
        hi,
        set the datatextfield property to the name of the course
        set the datavaluefield property to the id as shown below

        ddlist.DataText Field = "coursename "
        ddlist.DataValu eField = "course_id"

        to access the id use the following code

        ddlist.Selected Item.Value
        using the above code only the course name is displayed and internally u get the course id

        hope this helps u
        Cheers!
        Asha S :)

        Comment

        • icetalks
          New Member
          • Feb 2007
          • 8

          #5
          Originally posted by ashasprabhu
          hi,
          set the datatextfield property to the name of the course
          set the datavaluefield property to the id as shown below

          ddlist.DataText Field = "coursename "
          ddlist.DataValu eField = "course_id"

          to access the id use the following code

          ddlist.Selected Item.Value
          using the above code only the course name is displayed and internally u get the course id

          hope this helps u
          Cheers!
          Asha S :)

          Thnx .. but as i said , i am using ComboBox not list box .. .. sorry , i didn't understand it .. have a look at my code :

          ' this sub to get the database from the SQL and fill it into the combo box.

          Private Sub KG_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
          Dim cn As New SqlConnection
          cn.ConnectionSt ring = "Data Source=PC100-042295F4E;" & "Trusted_connec tion=yes;" & "initial Catalog=Client; "
          Dim da As New SqlDataAdapter( "SELECT course_name as NAME, course_id FROM Courses WHERE (course_categor y = 5)", cn)
          Dim dt As New DataTable
          loading = True
          Try
          da.Fill(dt)
          Catch ex As Exception
          MessageBox.Show (ex.Message)
          End Try

          cmbKg.DataSourc e = dt
          cmbKg.DisplayMe mber = ("NAME") 'how to display the full name ? '& " " & "Emp_Sname" & " " & "Emp_Fname" )
          cmbKg.ValueMemb er = "course_id"
          loading = False
          End Sub
          ' now my code to fill from the combo box into another table in the data bas as below :

          Dim conn As New SqlConnection
          Dim cmd As New SqlCommand
          cmd.CommandText = "Insert into Clients_Tb (Course_id, .... values ( ??? here what shall i say !!?

          Comment

          • ashasprabhu
            New Member
            • Jan 2007
            • 52

            #6
            hi,
            try this code
            'Retrieve the data into a DataSet:
            Dim strSQL As String = "Select course_id,cours eName From MyTable"
            Dim Connection As New OleDbConnection ("PROVIDER=.... ")
            Dim DA As New OleDbDataAdapte r(strSQL, Connection)
            Dim DS As New DataSet
            DA.Fill(DS, "couse")

            'Create and populate the DataTable to bind to the ComboBox:
            Dim dt As New DataTable
            dt.Columns.Add( "cousename" , GetType(System. String))
            dt.Columns.Add( "couse_id", GetType(System. String))

            ' Populate the DataTable to bind to the Combobox.
            Dim drDSRow As DataRow
            Dim drNewRow As DataRow
            For Each drDSRow In DS.Tables("cous e").Rows()
            drNewRow = dt.NewRow()
            drNewRow("couse name") = drDSRow("cousen ame")
            drNewRow("couse _id") = drDSRow("couse_ id")
            dt.Rows.Add(drN ewRow)
            Next

            ComboBox1.DropD ownStyle = ComboBoxStyle.D ropDownList

            With ComboBox1
            .DataSource = dt
            .DisplayMember = "coursename "
            .ValueMember = "course_id"
            .SelectedIndex = 0
            End With
            'To select an item based on the "couse_id" or ValueMember property:
            Dim conn As New SqlConnection
            Dim cmd As New SqlCommand
            cmd.CommandText = "Insert into Clients_Tb (Course_id, .... values ("& ComboBox1.Value Member &".......... ..

            hope the above code gives u some idea

            Comment

            Working...