CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Filling combobox and radiobutton with sql information

  1. #1
    Join Date
    Mar 2020
    Posts
    6

    Exclamation Filling combobox and radiobutton with sql information

    Hi,

    I'm tryinng to fill a combobox and radiobutton with the info that i have insert in my sql database.
    I what to display the name of "Agente" in the combobox when i click in the "id" that will fill all textbox's, combobox!
    This is the code i'm trying:

    Code:
    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
            Try
                con.Open()
                i = Convert.ToInt32(DataGridView1.SelectedCells.Item(0).Value.ToString())
                cmd = con.CreateCommand()
                cmd.CommandType = CommandType.Text
                cmd.CommandText = "Select Agentes.[Id], Agentes.[Nome],Agentes.[Email],Agentes.[Localidade], Lojas.[Nome] as Loja, Agentes.[Anotacoes]
                                    From [TesteAna].[dbo].[Agentes]
                                    Inner join [TesteAna].[dbo].[Lojas] on Lojas.Id= Agentes.Loja 
                                    where Agentes.[Id]=" & i & ""
                cmd.ExecuteNonQuery()
                Dim dt As New DataTable()
                Dim da As New SqlDataAdapter(cmd)
                da.Fill(dt)
                Dim dr As SqlDataReader
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
                While dr.Read
                    TextBox1.Text = dr.GetString(1)
                    TextBox2.Text = dr.GetString(2)
                    TextBox3.Text = dr.GetString(3)
                    TextBox4.Text = dr.GetString(4)
                    TextBox5.Text = dr.GetString(5)
                    'ComboBox1.SelectedItem = dr.GetString(6) -- 'were is where i'm trying to fill combobox!
                End While
            Catch ex As Exception
                'MsgBox(ex.Message)
            End Try
        End Sub
    Can u help me with this problem?
    Regards!
    Last edited by 2kaud; May 12th, 2020 at 01:05 PM. Reason: Added code tags

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,257

    Re: Filling combobox and radiobutton with sql information

    [Thread moved from C++ forum as VB code. When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#'].

    Cheers!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.7.3)

  3. #3
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,786

    Re: Filling combobox and radiobutton with sql information

    Quote Originally Posted by Goncalof7 View Post
    Hi,

    I'm tryinng to fill a combobox and radiobutton with the info that i have insert in my sql database.
    I what to display the name of "Agente" in the combobox when i click in the "id" that will fill all textbox's, combobox!
    ...

    Can u help me with this problem?
    Regards!
    Does your code compile?
    If there was no problem with compiling then did you try do debug it to be sure you get the correct data from SQL database?
    Victor Nijegorodov

  4. #4
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,354

    Re: Filling combobox and radiobutton with sql information

    Your commented line of code tries to select an item in a combo box but you say you are trying to fill it. You have to add the items to the combo before you can select one.
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Mar 2020
    Posts
    6

    Re: Filling combobox and radiobutton with sql information

    Hi VictorN,

    Yes i can compile and debug without no problem, but when i try to fill the datagridview only textbox's get the information.
    Combobox doesn't change!

    Regards.

  6. #6
    Join Date
    Mar 2020
    Posts
    6

    Re: Filling combobox and radiobutton with sql information

    DataMister,

    Can you explain better please ou give a example?

    Regards.

  7. #7
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,354

    Re: Filling combobox and radiobutton with sql information

    You only show one line of code that references a combobox. That line is commented out, It tries to select an item in the combo box. If there is nothing in the combo it will give an error. You code does not add any items to the combo box.

    If you want to add that item(6) to the combo box then rather than using selected item you need to use items.add
    Always use [code][/code] tags when posting code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)