Solved

Binding to a Drop Down box with a sql server datasource

Posted on 2012-12-26
4
625 Views
Last Modified: 2012-12-26
I'm starting to play with JQuery a little and I would like to bind data from a sql server to a drop down box (just to learn how).  The code behind has to be in vb.net.

I have looked at several examples on the web but they either C# or vb.net and don't work/aren't complete.  Does anyone know of a good walkthrough that I can play with?

Thanks in advanced!
0
Comment
Question by:badrhino
  • 2
  • 2
4 Comments
 
LVL 18

Expert Comment

by:Rajar Ahmed
ID: 38721613
Try these,

Approach 1 . Binding dropdown using vb.net


Protected Sub Button1_Click1(sender As Object, e As EventArgs)

dim str As String = "Data Source=.;uid=sa;pwd=wintellect;database=rohatash"

	Dim con As New SqlConnection(str)
Dim anyFilter as string= ""
	Dim cmd As New SqlCommand("select textColumn,valueColumn from UserDetail where id = '" & anyFilter & "'", con)

	Dim Adpt As New SqlDataAdapter(cmd)

	Dim dt As New DataTable()

	Adpt.Fill(dt)

	ddl1.DataSource = dt

	ddl1.DataBind()
ddl1.DataTextField="textColumn"
ddl1.DataValueField="valueColumn"


End Sub

Open in new window

Approach 2 : Using SqlDatasource control .
Binding Dropdown using SqlDatasource control without codebehind .
http://asp-net-example.blogspot.in/2008/12/aspnet-sqldatasource-example-using.html
Sqldatasource connectionstring parameter is configured in web.config file .
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
<connectionStrings>
  <add 
    name="NorthwindConnectionString" 
    connectionString="Data Source=serverName;Initial 
    Catalog=Northwind;Persist Security Info=True;User 
    ID=userName;Password=password"
    providerName="System.Data.SqlClient"
  />
</connectionStrings>

Open in new window


More Info Regarding Connection string ,
http://msdn.microsoft.com/en-us/library/ms178411(v=vs.100).aspx

Meeran03
0
 
LVL 1

Author Comment

by:badrhino
ID: 38721649
This works great for server side code, but how do you do it with JQuery?
0
 
LVL 18

Accepted Solution

by:
Rajar Ahmed earned 500 total points
ID: 38721687
You are looking for AJAX using jquery,
The below example matches  your requirement .
http://www.aspdotnet-suresh.com/2012/07/how-to-bind-dropdownlist-in-aspnet.html
Walkthrough:
1.Use Jquery Ajax functionality
2.Create WebMethod in your page and assign it on the url of jquery ajax .
3.Webmethod returns array .
4.Loop through the array and bind the dropdown on success of jquery ajax ,

More Advanced.
http://www.aspsnippets.com/Articles/AJAX-Cascading-DropDownList-using-jQuery-in-ASP.Net.aspx

Meeran03
0
 
LVL 1

Author Closing Comment

by:badrhino
ID: 38721857
Perfect. Thanks!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now