Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi I'm a new to vb.net ,my question is I am Using a Combo box and I need to get the data from sql server using connection string and I have tried the below code.It is not working

What I have tried:

VB
Imports System
Imports System.Collections.Generic
Imports System.Web
Imports System.Xml
Imports System.Windows.Forms
Imports System.Data.SqlClient
Imports System.Configuration


Public Class Form1

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim VB = ConfigurationManager.ConnectionString["HMDB"].ConnectionString
        Using Con = New SqlConnection(VB)
            Dim SqlText = "Select DocEntry from dbo.Master1"
            Dim cmd = New SqlCommand(SqlText, Con)
            Con.Open()
            ComboBox1.DataSource = cmd.ExecuteReader()
            ComboBox1.DataBind()

        End Using
    End Sub
End Class


______________________________________________________________________________________
App.config


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionstrings>
  <connectionStrings>
      <add name ="HMDB"
           connectionString ="Data Source=DESKTOP-68VGRJG;Database=master;integrated security =SSPI"/>
            </connectionStrings>
</configuration>
</configuration>
Posted
Updated 23-Oct-16 21:52pm
v2
Comments
F-ES Sitecore 24-Oct-16 4:25am    
"Not working" has no technical definition or meaning, you will have to explain what the issue is a bit better. ie do you get error messages, what do you see that you wouldn't expect, what do you not see that you do expect etc. However I feel you need to learn to use the debugger to step through your code, and also google how to bind an asp.net combo box to the database as each item in a dropdown needs both a text field and a value field (you only have one field in your data source) and you also need to tell the dropdown the name of the fields you want to use for text\value, look at the code here

http://stackoverflow.com/questions/15205380/how-to-bind-a-drop-down-control-to-a-data-source-in-asp-net
Member 12605293 24-Oct-16 9:08am    
Hi
Thanks for your quick reply ,I will read the link.All I need is to get the data from a particular column in my database to get transferred to ComboBox.But it is not giving the output ie no data is shown , when I click the drop down in my combo box.

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900