Click here to Skip to main content
15,888,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am here with simple question.

I have a screen with three combo Box.

I have a table with name tbCountries in my sql 2008.


now i want to papulate the countries from my database.and drop down my combo box.

how can i do this?
Posted
Comments
Rajesh Kariyavula 31-May-12 4:03am    
Have you tried anything so far...?
prince_rumeel 31-May-12 4:05am    
basically i dont know how can i connect to databse.in asp.net

in vb.net we can do

Dim conn As sqlconnection


Dim strSQL As String = "SELECT * FROM Disk"
Dim da As New SqlDataAdapter(strSQL, conn)
Dim ds As New DataSet
da.Fill(ds, "Disk")

With ComboBox1
.DataSource = ds.Tables("Disk")
.DisplayMember = "Disk_Name"
.ValueMember = "Disk_Key"
.SelectedIndex = 0
End With



but i dont know how can i connect with my database using asp.net
Ed Nutting 31-May-12 4:16am    
Errr..what? I think you need to do some serious basic research on what ASP.Net and VB.Net are. ASP.Net refers to the web system but VB.Net is the programming language. Using ASP.Net has no effect on what the VB.Net language can do, it's just what environment your running the VB.Net code in. You can use just the same code to connect to your database in ASP.Net as you would in Windows Forms - they are both VB.Net.
Ed
AmitGajjar 31-May-12 6:12am    
Hi,

Never learn language as solution oriented. read some articles, ebooks , and implement the sample given in the books. then only start working on any project. that will save your lots of time.

-Amit

1 solution

hi,
try like this:
C#
public void FillComboBox()
   {
       String ConnStr = "Data Source=ServerName;Initial Catalog=DBNamae;User ID=UserName;Password='password';";
       String SQL = "SELECT * FROM Countries";
       SqlDataAdapter ad = new SqlDataAdapter(SQL, ConnStr);
       DataSet ds = new DataSet();
       ad.Fill(ds);

       comboBox1.DataSource = ds.Tables[0];
       comboBox1.DisplayMember = "CountryName";
   }
 
Share this answer
 
Comments
prince_rumeel 31-May-12 4:30am    
I am using Dex Exp Controls
prince_rumeel 31-May-12 4:39am    
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data.SqlClient
Imports System.Configuration
Partial Class Fill_Combos

Inherits System.Web.UI.Page
Private cmd As New SqlCommand()
Private con As New SqlConnection()



Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' here i am declare connection
con.ConnectionString = ConfigurationManager.ConnectionStrings("cn").ConnectionString
con.Open()
If Page.IsPostBack = False Then

' here i am calling function chart_bind(); in the page load event of the page
FillComboBox()
End If
End Sub

Public Sub FillComboBox()
cmd = New SqlCommand()
' here inside the cmd i am definning sql query to select the name from the table
cmd.CommandText = "select * from tbStates"
cmd.Connection = con
' here i am declairing the SqlDataReader to catch the values that will come from the sql query
Dim dd_values As SqlDataReader
' here am declairing the ExecuteReader to execute SqlDataReade
dd_values = cmd.ExecuteReader()
' here i am binding the dropdownlist with the SqlDataReader
dd_country.DataSource = dd_values
' here i am binding the dropdownlist with the cloumn name of the table to show the
' name inside the dropdownlist
'dd_country.DataValueField = "CountryName"
'dd_country.DataBind
dd_country.DataBind()
' after binding the dropdown list here i am closing and disposing the SqlDataReader
dd_values.Dispose()
dd_values.Close()
End Sub
End Class



I am trying like this one.


what you say.am i going in right direction?
tanweer 31-May-12 6:36am    
in this particular case instead of using SqlDataReader use SqlDataAdapter like I have used in my solution. If you use SqlDataReader then you have to loop through SqlDataReader

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