Showing posts with label dropdown. Show all posts
Showing posts with label dropdown. Show all posts

Thursday, March 29, 2012

Poplulating Text Field with DropDown selection

Hi folks. Here's a simple question. Trying to populate some text fields based on a dropdown menu selection, but it doesn't work. When a selection is made in the dropdown menu, it seems to default to the selectedValue =2. Not sure why. Any advice would be much appreciated. Thanks! Here's the code:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { TextBox1.Text =""; TextBox2.Text =""; TextBox3.Text ="";if (DropDownList1.SelectedValue =="0") TextBox1.Text ="blah";if (DropDownList1.SelectedValue =="1") TextBox1.Text ="blahblah";if (DropDownList1.SelectedValue =="2") TextBox1.Text ="blahblahblah"; }

Are you dynamicalluy populating the dropdown list in your code behind? If you are, the Page_OnLoad event is running which is populating your dropdownlist on every postback. To make sure this doesnt happen, you should populate when teh page is not posted back. in other words...

if(!Page.IsPostback){// populate my dropdownlist}

If you are not dynamically populating the dropdownlist, please make sure your viewstate is turned on, that way the server knows that index to preserve when the page is loaded again.

EnableViewState="true"

Hope this helps.


If you are adding the items to the dropdown through code in Page_Load then check if they are inside If (!Ispostback) block.


hi..

i think u can have a break point in the page load or start of selected index changed event..

and find the value..

if not..

check in the design view that item 2 isSelected=true..

correct me if i'm wrong..


Thanks to all for the suggestions. The dropDownList was statically populated from the control properties in the ASPX page. I tinkered with this for a long while, but could NOT get it to work. So, I changed approach and populated the list by making an array in the code, and now it works as designed. Thanks again. Here is the code, in case anybody is interested.

protected void Page_Load(object sender, EventArgs e) {if (!IsPostBack) {// 2 dimensional array for dropDown liststring[,] forms = { {"blah","0"}, {"blah blah","1"}, {"blah blah blah","2"} };// populate listfor (int i = 0; i < forms.GetLength(0); i++) {//add both text and value DropDownList1.Items.Add(new ListItem(forms[i, 0], forms[i, 1])); } } }protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) {if (DropDownList1.SelectedIndex != -1) { TextBox1.Text = DropDownList1.SelectedItem.Text; } }

FYI... regarding the orginal post, the reason the textBox wasn't populated correctly was a simple syntax error. Used cascading IF statement without ELSE IF or switch statement.

Populate .net dropdown

I have 2 tables. One is country and holds a list of countries and the other
is region which holds a list of states/provinces/countys relating to that
country.

How do I populate a combo so that it is indented.

e.g

USA
-Florida
-California
-Utah
Canada
-Ontario
-Quebec
England
-Kent
-Northumberland

TIAYou get real familiar with the dropdownlist.add() method, because this isn't
something you are going to acheive with the standard way of doing things.
First you have to design a query to return
countries/states/provinces/countys sorted by country, then you have to loop
through them, set some variable to the current country, and then pop either
a Country, or a state/province/county in the dropdownlist using the .add
method. If it is a state/province/county you'll probably need to add
something to the beginning to actually indent it. Then you'll have the
problem of finding out if they selected a country or a
state/province/county.

sounds fun!

"Poppy" <paul.diamond@.NOSPAMthemedialounge.com> wrote in message
news:%23ndPZP32DHA.556@.TK2MSFTNGP11.phx.gbl...
> I have 2 tables. One is country and holds a list of countries and the
other
> is region which holds a list of states/provinces/countys relating to that
> country.
> How do I populate a combo so that it is indented.
> e.g
> USA
> -Florida
> -California
> -Utah
> Canada
> -Ontario
> -Quebec
> England
> -Kent
> -Northumberland
> TIA

Populate 2nd dropdown list based on selecteditem in first with out postback?

Hi All:

I have an aspx page that has 2 dropdownlists. I want to populate the second
dropdownlist based on the selecteditem in the first dropdown. I know I can
do this by doing a post back and using the selectedindex and loading the
second dropdownlist.

Is there anyway I can do this without doing a postback? Any ideas/pointers
will be much appreciated.

Thanks!

VinayYou'll have to use JavaScript in the client.

search: dependent listbox

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/

"Vinay" <vinay_hs_removethis@.nospam.yahoo.com> wrote in message
news:ef$cOw4oFHA.2976@.TK2MSFTNGP12.phx.gbl...
> Hi All:
> I have an aspx page that has 2 dropdownlists. I want to populate the
> second dropdownlist based on the selecteditem in the first dropdown. I
> know I can do this by doing a post back and using the selectedindex and
> loading the second dropdownlist.
> Is there anyway I can do this without doing a postback? Any ideas/pointers
> will be much appreciated.
> Thanks!
> Vinay
Thanks for the pointer - will do some reading and see if I can implement it.

"clintonG" <csgallagher@.REMOVETHISTEXTmetromilwaukee.com> wrote in message
news:ek$zp%234oFHA.3656@.TK2MSFTNGP09.phx.gbl...
> You'll have to use JavaScript in the client.
> search: dependent listbox
> <%= Clinton Gallagher
> METROmilwaukee (sm) "A Regional Information Service"
> NET csgallagher AT metromilwaukee.com
> URL http://metromilwaukee.com/
> URL http://clintongallagher.metromilwaukee.com/
>
> "Vinay" <vinay_hs_removethis@.nospam.yahoo.com> wrote in message
> news:ef$cOw4oFHA.2976@.TK2MSFTNGP12.phx.gbl...
>> Hi All:
>>
>> I have an aspx page that has 2 dropdownlists. I want to populate the
>> second dropdownlist based on the selecteditem in the first dropdown. I
>> know I can do this by doing a post back and using the selectedindex and
>> loading the second dropdownlist.
>>
>> Is there anyway I can do this without doing a postback? Any
>> ideas/pointers will be much appreciated.
>>
>> Thanks!
>>
>> Vinay
>>

Populate 2nd dropdown list based on selecteditem in first with out postback?

Hi All:
I have an aspx page that has 2 dropdownlists. I want to populate the second
dropdownlist based on the selecteditem in the first dropdown. I know I can
do this by doing a post back and using the selectedindex and loading the
second dropdownlist.
Is there anyway I can do this without doing a postback? Any ideas/pointers
will be much appreciated.
Thanks!
VinayYou'll have to use JavaScript in the client.
search: dependent listbox
<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/
"Vinay" <vinay_hs_removethis@.nospam.yahoo.com> wrote in message
news:ef$cOw4oFHA.2976@.TK2MSFTNGP12.phx.gbl...
> Hi All:
> I have an aspx page that has 2 dropdownlists. I want to populate the
> second dropdownlist based on the selecteditem in the first dropdown. I
> know I can do this by doing a post back and using the selectedindex and
> loading the second dropdownlist.
> Is there anyway I can do this without doing a postback? Any ideas/pointers
> will be much appreciated.
> Thanks!
> Vinay
>
Thanks for the pointer - will do some reading and see if I can implement it.
"clintonG" < csgallagher@.REMOVETHISTEXTmetromilwaukee
.com> wrote in message
news:ek$zp%234oFHA.3656@.TK2MSFTNGP09.phx.gbl...
> You'll have to use JavaScript in the client.
> search: dependent listbox
> <%= Clinton Gallagher
> METROmilwaukee (sm) "A Regional Information Service"
> NET csgallagher AT metromilwaukee.com
> URL http://metromilwaukee.com/
> URL http://clintongallagher.metromilwaukee.com/
>
> "Vinay" <vinay_hs_removethis@.nospam.yahoo.com> wrote in message
> news:ef$cOw4oFHA.2976@.TK2MSFTNGP12.phx.gbl...
>

Monday, March 26, 2012

Populate a dropdown list with a variable from a Querystring

Hi,

I'm passing a variable to another page through a querystring. I then want
to use that variable to retrieve records from a database to poulate a
dropdownlist. I can read the variable from the querystring but I'm not sure
how to pass that value. I get the error that IntCourseID is not declared in:

<asp:DropDownList id="fLessonID" runat="server" DataValueField="LessonID"
DataSource="<%# GetLessons(IntCourseID) %>"

I tried moving the querystring outside the page load but that didn't work:

This is the code I have:

<%@dotnet.itags.org. Page Language="VB" Debug="true" validaterequest="false" %
<script runat="server"
Sub Page_Load(sender As Object, e As EventArgs)
If Page.IsPostBack = False Then
Dim IntCourseID as Integer
IntCourseID = Request.QueryString( "CourseID" )

End If
End Sub

Function GetLessons(ByVal courseID As Integer) As
System.Data.IDataReader
Dim connectionString As String = "server='(local)';
trusted_connection=true; database='xx'"
Dim dbConnection As System.Data.IDbConnection = New
System.Data.SqlClient.SqlConnection(connectionStri ng)

Dim queryString As String = "SELECT [tblLesson].[LessonID],
[tblLesson].[LessonTitle] FROM [tblLesson] WHERE ("& _
"[tblLesson].[CourseID] = @dotnet.itags.org.CourseID)"
Dim dbCommand As System.Data.IDbCommand = New
System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

Dim dbParam_courseID As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_courseID.ParameterName = "@dotnet.itags.org.CourseID"
dbParam_courseID.Value = courseID
dbParam_courseID.DbType = System.Data.DbType.Int32
dbCommand.Parameters.Add(dbParam_courseID)

dbConnection.Open
Dim dataReader As System.Data.IDataReader =
dbCommand.ExecuteReader(System.Data.CommandBehavio r.CloseConnection)

Return dataReader
End Function

Sub addButton_Click(sender As Object, e As EventArgs)
Response.Redirect("selectPage.aspx")
End Sub

</script>
<html>
<head>
</head>
<body leftmargin="0" topmargin="0">
<form runat="server">
<asp:DropDownList id="fLessonID" runat="server"
DataValueField="LessonID" DataSource="<%# GetLessons(IntCourseID) %>"
DataTextField="LessonTitle"></asp:DropDownList>
<asp:Button id="addButton" onclick="addButton_Click" runat="server"
Text="Next"></asp:Button
</form>
</body>
</html
Thanks for any help

--
Message posted via http://www.dotnetmonster.comHi Jim,

I understood your probleim in the following way after reading your code...

You have a Integer Variable (IntCourseID) in your code behind fi.e...and you
want to use that value in your .aspx page. Am I right...?

If I'm right...then solution for your problem is...

Have a hidden control...and assign your querystring value (CourseId) to the
value of the hidden control. Now you can use that control in your .aspx page
code, to bind with the ddl.

Cheers,

Jerome. M

"Jim via DotNetMonster.com" wrote:

> Hi,
> I'm passing a variable to another page through a querystring. I then want
> to use that variable to retrieve records from a database to poulate a
> dropdownlist. I can read the variable from the querystring but I'm not sure
> how to pass that value. I get the error that IntCourseID is not declared in:
> <asp:DropDownList id="fLessonID" runat="server" DataValueField="LessonID"
> DataSource="<%# GetLessons(IntCourseID) %>"
> I tried moving the querystring outside the page load but that didn't work:
> This is the code I have:
> <%@. Page Language="VB" Debug="true" validaterequest="false" %>
> <script runat="server">
> Sub Page_Load(sender As Object, e As EventArgs)
> If Page.IsPostBack = False Then
> Dim IntCourseID as Integer
> IntCourseID = Request.QueryString( "CourseID" )
> End If
> End Sub
>
> Function GetLessons(ByVal courseID As Integer) As
> System.Data.IDataReader
> Dim connectionString As String = "server='(local)';
> trusted_connection=true; database='xx'"
> Dim dbConnection As System.Data.IDbConnection = New
> System.Data.SqlClient.SqlConnection(connectionStri ng)
> Dim queryString As String = "SELECT [tblLesson].[LessonID],
> [tblLesson].[LessonTitle] FROM [tblLesson] WHERE ("& _
> "[tblLesson].[CourseID] = @.CourseID)"
> Dim dbCommand As System.Data.IDbCommand = New
> System.Data.SqlClient.SqlCommand
> dbCommand.CommandText = queryString
> dbCommand.Connection = dbConnection
> Dim dbParam_courseID As System.Data.IDataParameter = New
> System.Data.SqlClient.SqlParameter
> dbParam_courseID.ParameterName = "@.CourseID"
> dbParam_courseID.Value = courseID
> dbParam_courseID.DbType = System.Data.DbType.Int32
> dbCommand.Parameters.Add(dbParam_courseID)
> dbConnection.Open
> Dim dataReader As System.Data.IDataReader =
> dbCommand.ExecuteReader(System.Data.CommandBehavio r.CloseConnection)
> Return dataReader
> End Function
> Sub addButton_Click(sender As Object, e As EventArgs)
> Response.Redirect("selectPage.aspx")
> End Sub
> </script>
> <html>
> <head>
> </head>
> <body leftmargin="0" topmargin="0">
> <form runat="server">
> <asp:DropDownList id="fLessonID" runat="server"
> DataValueField="LessonID" DataSource="<%# GetLessons(IntCourseID) %>"
> DataTextField="LessonTitle"></asp:DropDownList>
> <asp:Button id="addButton" onclick="addButton_Click" runat="server"
> Text="Next"></asp:Button>
> </form>
> </body>
> </html>
> Thanks for any help
> --
> Message posted via http://www.dotnetmonster.com
Hello
Dear Jim!
First the problem with your code is that you are declaring a Variable in
pageload event "IntCourseID" that will be destroyed when this event will
reach its end.
First Declare this variable in the GetLessons function
IntCourseID = Request.QueryString( "CourseID" )
so that this variable with its value can be accessed.

Regards
Malik Asif
ASP.NET,VB.NET

"Jim via DotNetMonster.com" <forum@.DotNetMonster.com> wrote in message
news:8596ac9543c84e2e8aad88fcc163c277@.DotNetMonste r.com...
> Hi,
> I'm passing a variable to another page through a querystring. I then want
> to use that variable to retrieve records from a database to poulate a
> dropdownlist. I can read the variable from the querystring but I'm not
sure
> how to pass that value. I get the error that IntCourseID is not declared
in:
> <asp:DropDownList id="fLessonID" runat="server" DataValueField="LessonID"
> DataSource="<%# GetLessons(IntCourseID) %>"
> I tried moving the querystring outside the page load but that didn't work:
> This is the code I have:
> <%@. Page Language="VB" Debug="true" validaterequest="false" %>
> <script runat="server">
> Sub Page_Load(sender As Object, e As EventArgs)
> If Page.IsPostBack = False Then
> Dim IntCourseID as Integer
> IntCourseID = Request.QueryString( "CourseID" )
> End If
> End Sub
>
> Function GetLessons(ByVal courseID As Integer) As
> System.Data.IDataReader
> Dim connectionString As String = "server='(local)';
> trusted_connection=true; database='xx'"
> Dim dbConnection As System.Data.IDbConnection = New
> System.Data.SqlClient.SqlConnection(connectionStri ng)
> Dim queryString As String = "SELECT [tblLesson].[LessonID],
> [tblLesson].[LessonTitle] FROM [tblLesson] WHERE ("& _
> "[tblLesson].[CourseID] = @.CourseID)"
> Dim dbCommand As System.Data.IDbCommand = New
> System.Data.SqlClient.SqlCommand
> dbCommand.CommandText = queryString
> dbCommand.Connection = dbConnection
> Dim dbParam_courseID As System.Data.IDataParameter = New
> System.Data.SqlClient.SqlParameter
> dbParam_courseID.ParameterName = "@.CourseID"
> dbParam_courseID.Value = courseID
> dbParam_courseID.DbType = System.Data.DbType.Int32
> dbCommand.Parameters.Add(dbParam_courseID)
> dbConnection.Open
> Dim dataReader As System.Data.IDataReader =
> dbCommand.ExecuteReader(System.Data.CommandBehavio r.CloseConnection)
> Return dataReader
> End Function
> Sub addButton_Click(sender As Object, e As EventArgs)
> Response.Redirect("selectPage.aspx")
> End Sub
> </script>
> <html>
> <head>
> </head>
> <body leftmargin="0" topmargin="0">
> <form runat="server">
> <asp:DropDownList id="fLessonID" runat="server"
> DataValueField="LessonID" DataSource="<%# GetLessons(IntCourseID) %>"
> DataTextField="LessonTitle"></asp:DropDownList>
> <asp:Button id="addButton" onclick="addButton_Click"
runat="server"
> Text="Next"></asp:Button>
> </form>
> </body>
> </html>
> Thanks for any help
> --
> Message posted via http://www.dotnetmonster.com

Populate A Dropdown From Another Dropdown

I have a page that has two dropdowns on it. When the page loads, the first box is populated from an Access table. Here's the tricky part for me...

(Just when I think I understand how "postback" works, I find I'm wrong again!)

I have the dropdown set to autopostback=true. When the user selects a choice in this dropdown, the page postsback (I think) and I want it to then query another table in the same database to fill the second dropdown list AND keep the original dropdown list's selection.

For some reason, I can get it to fill the original dropdown, but then when it postsback, I get an error because my SQL statement to load the second dropdown looks like this:

Dim objSeriesDA As New OleDb.OleDbDataAdapter("SELECT * FROM Series WHERE fldProviderID=" & lstProvider.SelectedItem.Value & " ORDER BY fldSeriesNumber DESC", objSeriesCN)

Apparently, when the page postsback, the original dropdown list loses its selection, causing the SQL statement above to error out on a null reference.

Any ideas?

If need be, I can throw this up on a server so you all can see what I'm talking about... I can post the full source as well.

Thanks!

HWKY
:)wrap the code that is filling your first drop down list with this:

If Not Page.IsPostBack Then
'...your code here.
End If
Does anyone have a link to some information that explains PostBack in depth?
well, this link (http://www.15seconds.com/Issue/020102.htm) helped me with some basics on the page lifecycle when i was having user control postback problems. Really, there's not much more to the kind of postback stuff in your first post. If you know how the page loads and when control events are evaluated, that's pretty much it. Were you looking for information on how to roll your own PostBack handler or something else?

Populate a dropdown list with a variable from a Querystring

Hi,
I'm passing a variable to another page through a querystring. I then want
to use that variable to retrieve records from a database to poulate a
dropdownlist. I can read the variable from the querystring but I'm not sure
how to pass that value. I get the error that IntCourseID is not declared in:
<asp:DropDownList id="fLessonID" runat="server" DataValueField="LessonID"
DataSource="<%# GetLessons(IntCourseID) %>"
I tried moving the querystring outside the page load but that didn't work:
This is the code I have:
<%@dotnet.itags.org. Page Language="VB" Debug="true" validaterequest="false" %>
<script runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
If Page.IsPostBack = False Then
Dim IntCourseID as Integer
IntCourseID = Request.QueryString( "CourseID" )
End If
End Sub
Function GetLessons(ByVal courseID As Integer) As
System.Data.IDataReader
Dim connectionString As String = "server='(local)';
trusted_connection=true; database='xx'"
Dim dbConnection As System.Data.IDbConnection = New
System.Data.SqlClient.SqlConnection(connectionString)
Dim queryString As String = "SELECT [tblLesson].[LessonID],
[tblLesson].[LessonTitle] FROM [tblLesson] WHERE ("& _
"[tblLesson].[CourseID] = @dotnet.itags.org.CourseID)"
Dim dbCommand As System.Data.IDbCommand = New
System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection
Dim dbParam_courseID As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_courseID.ParameterName = "@dotnet.itags.org.CourseID"
dbParam_courseID.Value = courseID
dbParam_courseID.DbType = System.Data.DbType.Int32
dbCommand.Parameters.Add(dbParam_courseID)
dbConnection.Open
Dim dataReader As System.Data.IDataReader =
dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
Return dataReader
End Function
Sub addButton_Click(sender As Object, e As EventArgs)
Response.Redirect("selectPage.aspx")
End Sub
</script>
<html>
<head>
</head>
<body leftmargin="0" topmargin="0">
<form runat="server">
<asp:DropDownList id="fLessonID" runat="server"
DataValueField="LessonID" DataSource="<%# GetLessons(IntCourseID) %>"
DataTextField="LessonTitle"></asp:DropDownList>
<asp:Button id="addButton" onclick="addButton_Click" runat="server"
Text="Next"></asp:Button>
</form>
</body>
</html>
Thanks for any help
Message posted via http://www.webservertalk.comHi Jim,
I understood your probleim in the following way after reading your code...
You have a Integer Variable (IntCourseID) in your code behind fi.e...and you
want to use that value in your .aspx page. Am I right...?
If I'm right...then solution for your problem is...
Have a hidden control...and assign your querystring value (CourseId) to the
value of the hidden control. Now you can use that control in your .aspx page
code, to bind with the ddl.
Cheers,
Jerome. M
"Jim via webservertalk.com" wrote:

> Hi,
> I'm passing a variable to another page through a querystring. I then want
> to use that variable to retrieve records from a database to poulate a
> dropdownlist. I can read the variable from the querystring but I'm not sur
e
> how to pass that value. I get the error that IntCourseID is not declared i
n:
> <asp:DropDownList id="fLessonID" runat="server" DataValueField="LessonID"
> DataSource="<%# GetLessons(IntCourseID) %>"
> I tried moving the querystring outside the page load but that didn't work:
> This is the code I have:
> <%@. Page Language="VB" Debug="true" validaterequest="false" %>
> <script runat="server">
> Sub Page_Load(sender As Object, e As EventArgs)
> If Page.IsPostBack = False Then
> Dim IntCourseID as Integer
> IntCourseID = Request.QueryString( "CourseID" )
> End If
> End Sub
>
> Function GetLessons(ByVal courseID As Integer) As
> System.Data.IDataReader
> Dim connectionString As String = "server='(local)';
> trusted_connection=true; database='xx'"
> Dim dbConnection As System.Data.IDbConnection = New
> System.Data.SqlClient.SqlConnection(connectionString)
> Dim queryString As String = "SELECT [tblLesson].[LessonID],
> [tblLesson].[LessonTitle] FROM [tblLesson] WHERE ("& _
> "[tblLesson].[CourseID] = @.CourseID)"
> Dim dbCommand As System.Data.IDbCommand = New
> System.Data.SqlClient.SqlCommand
> dbCommand.CommandText = queryString
> dbCommand.Connection = dbConnection
> Dim dbParam_courseID As System.Data.IDataParameter = New
> System.Data.SqlClient.SqlParameter
> dbParam_courseID.ParameterName = "@.CourseID"
> dbParam_courseID.Value = courseID
> dbParam_courseID.DbType = System.Data.DbType.Int32
> dbCommand.Parameters.Add(dbParam_courseID)
> dbConnection.Open
> Dim dataReader As System.Data.IDataReader =
> dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
> Return dataReader
> End Function
> Sub addButton_Click(sender As Object, e As EventArgs)
> Response.Redirect("selectPage.aspx")
> End Sub
> </script>
> <html>
> <head>
> </head>
> <body leftmargin="0" topmargin="0">
> <form runat="server">
> <asp:DropDownList id="fLessonID" runat="server"
> DataValueField="LessonID" DataSource="<%# GetLessons(IntCourseID) %>"
> DataTextField="LessonTitle"></asp:DropDownList>
> <asp:Button id="addButton" onclick="addButton_Click" runat="server
"
> Text="Next"></asp:Button>
> </form>
> </body>
> </html>
> Thanks for any help
> --
> Message posted via http://www.webservertalk.com
>
Hello
Dear Jim!
First the problem with your code is that you are declaring a Variable in
pageload event "IntCourseID" that will be destroyed when this event will
reach its end.
First Declare this variable in the GetLessons function
IntCourseID = Request.QueryString( "CourseID" )
so that this variable with its value can be accessed.
Regards
Malik Asif
ASP.NET,VB.NET
"Jim via webservertalk.com" <forum@.webservertalk.com> wrote in message
news:8596ac9543c84e2e8aad88fcc163c277@.Do
webservertalk.com...
> Hi,
> I'm passing a variable to another page through a querystring. I then want
> to use that variable to retrieve records from a database to poulate a
> dropdownlist. I can read the variable from the querystring but I'm not
sure
> how to pass that value. I get the error that IntCourseID is not declared
in:
> <asp:DropDownList id="fLessonID" runat="server" DataValueField="LessonID"
> DataSource="<%# GetLessons(IntCourseID) %>"
> I tried moving the querystring outside the page load but that didn't work:
> This is the code I have:
> <%@. Page Language="VB" Debug="true" validaterequest="false" %>
> <script runat="server">
> Sub Page_Load(sender As Object, e As EventArgs)
> If Page.IsPostBack = False Then
> Dim IntCourseID as Integer
> IntCourseID = Request.QueryString( "CourseID" )
> End If
> End Sub
>
> Function GetLessons(ByVal courseID As Integer) As
> System.Data.IDataReader
> Dim connectionString As String = "server='(local)';
> trusted_connection=true; database='xx'"
> Dim dbConnection As System.Data.IDbConnection = New
> System.Data.SqlClient.SqlConnection(connectionString)
> Dim queryString As String = "SELECT [tblLesson].[LessonID],
> [tblLesson].[LessonTitle] FROM [tblLesson] WHERE ("& _
> "[tblLesson].[CourseID] = @.CourseID)"
> Dim dbCommand As System.Data.IDbCommand = New
> System.Data.SqlClient.SqlCommand
> dbCommand.CommandText = queryString
> dbCommand.Connection = dbConnection
> Dim dbParam_courseID As System.Data.IDataParameter = New
> System.Data.SqlClient.SqlParameter
> dbParam_courseID.ParameterName = "@.CourseID"
> dbParam_courseID.Value = courseID
> dbParam_courseID.DbType = System.Data.DbType.Int32
> dbCommand.Parameters.Add(dbParam_courseID)
> dbConnection.Open
> Dim dataReader As System.Data.IDataReader =
> dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
> Return dataReader
> End Function
> Sub addButton_Click(sender As Object, e As EventArgs)
> Response.Redirect("selectPage.aspx")
> End Sub
> </script>
> <html>
> <head>
> </head>
> <body leftmargin="0" topmargin="0">
> <form runat="server">
> <asp:DropDownList id="fLessonID" runat="server"
> DataValueField="LessonID" DataSource="<%# GetLessons(IntCourseID) %>"
> DataTextField="LessonTitle"></asp:DropDownList>
> <asp:Button id="addButton" onclick="addButton_Click"
runat="server"
> Text="Next"></asp:Button>
> </form>
> </body>
> </html>
> Thanks for any help
> --
> Message posted via http://www.webservertalk.com

Populate Data in DropDown

Hello All

Please help me .. I am stuck in a problem regarding DropDownList..

I want to populate 101690 records in DropDown List .. Although i Know
that it is not feasible.
but there must be some alternative solution for this. Please suggest
me ..

with regards
Thank youWhat are you trying to achieve? Likely, you are not on the right track.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]

"TARUN" <tarun.sinha@.gmail.comwrote in message
news:1153289215.578826.38220@.s13g2000cwa.googlegro ups.com...

Quote:

Originally Posted by

Hello All
>
Please help me .. I am stuck in a problem regarding DropDownList..
>
I want to populate 101690 records in DropDown List .. Although i Know
that it is not feasible.
but there must be some alternative solution for this. Please suggest
me ..
>
with regards
Thank you
>

Populate DataGrid from dynamic DropDown

Wow, it really sucks when you are trying to learn something and it just seems to not want to come together!

Anyways, my problem is this: I have a couple DropDownLists that are being populated from a database. Once the page loads, I am trying to set it up where the user can select an item from the DropDown, and then using a value associated the item in the DropDown, search the database again, and pull all matching records and place them in a DataGrid.

My code looks something like this:


<script runat="server"
Sub Page_Load(Source As Object, E As EventArgs)
If Not Page.IsPostBack Then
StatesDropDownList()
End IF
End Sub

Sub StatesDropDownList()

Dim strConnection As String = "Provider=Microsoft.Jet.OleDb.4.0;data source=" & Server.Mappath("\data\globalresorts.mdb")
Dim objConnection As New OleDbConnection (strConnection)
Dim strSqlStates As String = "SELECT DISTINCT State FROM Properties"

Dim objCommand As New OleDbCommand(strSqlStates, objConnection)
Dim objReader As OleDbDataReader = Nothing

objConnection.Open()
objReader = objCommand.ExecuteReader()

ddlStatesFilter.DataSource = objReader
ddlStatesFilter.DataTextField = "State"
ddlStatesFilter.DataValueField = "State"
ddlStatesFilter.DataBind()
objConnection.Close()
Dim DefaultState As ListItem = New ListItem
DefaultState.Text = "Select a State"
DefaultState.Value = "0"
ddlStatesFilter.Items.Insert(0, DefaultState)

End Sub

Sub DoSearch(Source as Object, E as EventArgs)

Dim strStates As String = ddlStatesFilter.SelectedItem.Value
Dim strConnection As String = "Provider=Microsoft.Jet.OleDb.4.0;data source=" & Server.Mappath("\data\globalresorts.mdb")
Dim objConnection As New OleDbConnection(strConnection)
Dim strSQL As String = "SELECT * FROM Properties WHERE State =" & ddlStatesFilter.SelectedItem.Value & " ORDER BY ID"

Dim objAdapter as New OleDbDataAdapter(strSQL, objConnection)

Dim objDataSet As New DataSet()
objAdapter.Fill(objDataSet, "dtProperties")

dgProperties.PagerStyle.NextPageText = "Next"
dgProperties.PagerStyle.PrevPageText = "Previous"

dgProperties.DataSource = objDataSet.Tables.Item("dtProperties")
dgProperties.DataBind()

End Sub
</script>

And then the control and the DataGrid look a little something like this:


<form runat="server>
<asp:literal ID="lbStatesFilter" runat="server" Text="State: " />
<asp:dropdownlist ID="ddlStatesFilter" runat="server" Font-Names="Verdana" Font-Size="8pt" />
<asp:button ID="SubmitSearch" runat="server" OnClick="DoSearch" Text="Search Listings" Font-Names="Verdana" Font-Size="8pt" Width="150" />
<asp:Datagrid id="dgProperties" runat="server"
AutoGenerateColumns="false"
Font-Size="8pt"
CellPadding="8"
AlternatingItemStyle-BackColor="#CCCCCC"
Font-Names="Verdana"
Width="100%"
AllowPaging="True"
OnPageIndexChanged="GridPageChange"
PageSize="20">
<HeaderStyle font-bold="True" forecolor="White" backcolor="#005BAC"></HeaderStyle>
<EditItemStyle backcolor="WhiteSmoke"></EditItemStyle>
<AlternatingItemStyle backcolor="Gainsboro"></AlternatingItemStyle>
<ItemStyle backcolor="WhiteSmoke"></ItemStyle>
<Columns>
<asp:BoundColumn DataField="ID" HeaderText="ID" DataFormatString="{0:N0}" />
<asp:hyperlinkcolumn HeaderText="Resort Name" datanavigateurlfield="ID" datanavigateurlformatstring="details.aspx?id={0}" datatextfield="Resort_Name" />
<asp:BoundColumn DataField="City" HeaderText="City" />
<asp:BoundColumn DataField="State" HeaderText="State" />
<asp:BoundColumn DataField="Bedrooms" HeaderText="Bedrooms" />
<asp:BoundColumn DataField="Week" HeaderText="Week" />
<asp:BoundColumn DataField="Sale_Price" HeaderText="Sale Price" DataFormatString="{0:c2}" />
<asp:BoundColumn DataField="Rent_Price" HeaderText="Rent Price" DataFormatString="{0:c2}" />
</Columns>
</asp:Datagrid>
</form>

And here is the error that I am getting when I try and run it:


Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: No value given for one or more required parameters.

Source Error:

Line 149:
Line 150: Dim objDataSet As New DataSet()
Line 151: objAdapter.Fill(objDataSet, "dtProperties")
Line 152:
Line 153:dgProperties.PagerStyle.NextPageText = "Next"

Source File: D:\...\search.aspx Line: 151

Thanks in advance for any help on this one. And sorry for so much code, but Im just not sure where the problem is coming from, so I want to make sure I give all the facts.

-JasonWithout looking at your code, basic concpets.
On page load, populate the drop down lists, enclose this in the If Page.IsPostBack = False, so it only happens once, obviously this can change based on your requirements.
Next, have a method that populates the datagrid, such as the dropdownlist selectedindexchange. Just use the selectedindex, value to hit the database to populate the datagrid.

populate ddl from two tables

Hello,
I have two Tables: Table1: GroupID1, GroupName1 and Table2:GroupID2,
GroupName2.
I have only one dropdown list (DropDownList1) in my application and through
a stored procedure I want to populate my DropDownList1 from GroupName1 and
GroupName2. How should I write my query?
Thanks,
Jim.You'll probably want to use a union.
select companyname from suppliers union select shipcity from orders
try this on Northwind, it'll return the companynames and shipcity in one
long list.
HTH
Pete
--
http://www.xboxracing.net/
"JIM.H." wrote:

> Hello,
> I have two Tables: Table1: GroupID1, GroupName1 and Table2:GroupID2,
> GroupName2.
> I have only one dropdown list (DropDownList1) in my application and throug
h
> a stored procedure I want to populate my DropDownList1 from GroupName1 and
> GroupName2. How should I write my query?
> Thanks,
> Jim.
>
Jim,
You need to join 2 tables with a UNION query.
Eliyahu
"JIM.H." <JIMH@.discussions.microsoft.com> wrote in message
news:318E29E9-6546-43E0-9B65-C0529E58FA96@.microsoft.com...
> Hello,
> I have two Tables: Table1: GroupID1, GroupName1 and Table2:GroupID2,
> GroupName2.
> I have only one dropdown list (DropDownList1) in my application and
through
> a stored procedure I want to populate my DropDownList1 from GroupName1 and
> GroupName2. How should I write my query?
> Thanks,
> Jim.
>
Can you give me an example?
"Eliyahu Goldin" wrote:

> Jim,
> You need to join 2 tables with a UNION query.
> Eliyahu
> "JIM.H." <JIMH@.discussions.microsoft.com> wrote in message
> news:318E29E9-6546-43E0-9B65-C0529E58FA96@.microsoft.com...
> through
>
>

populate ddl from two tables

Hello,
I have two Tables: Table1: GroupID1, GroupName1 and Table2:GroupID2,
GroupName2.

I have only one dropdown list (DropDownList1) in my application and through
a stored procedure I want to populate my DropDownList1 from GroupName1 and
GroupName2. How should I write my query?
Thanks,
Jim.You'll probably want to use a union.

select companyname from suppliers union select shipcity from orders

try this on Northwind, it'll return the companynames and shipcity in one
long list.

HTH

Pete
--
http://www.xboxracing.net/

"JIM.H." wrote:

> Hello,
> I have two Tables: Table1: GroupID1, GroupName1 and Table2:GroupID2,
> GroupName2.
> I have only one dropdown list (DropDownList1) in my application and through
> a stored procedure I want to populate my DropDownList1 from GroupName1 and
> GroupName2. How should I write my query?
> Thanks,
> Jim.
Jim,

You need to join 2 tables with a UNION query.

Eliyahu

"JIM.H." <JIMH@.discussions.microsoft.com> wrote in message
news:318E29E9-6546-43E0-9B65-C0529E58FA96@.microsoft.com...
> Hello,
> I have two Tables: Table1: GroupID1, GroupName1 and Table2:GroupID2,
> GroupName2.
> I have only one dropdown list (DropDownList1) in my application and
through
> a stored procedure I want to populate my DropDownList1 from GroupName1 and
> GroupName2. How should I write my query?
> Thanks,
> Jim.
Can you give me an example?

"Eliyahu Goldin" wrote:

> Jim,
> You need to join 2 tables with a UNION query.
> Eliyahu
> "JIM.H." <JIMH@.discussions.microsoft.com> wrote in message
> news:318E29E9-6546-43E0-9B65-C0529E58FA96@.microsoft.com...
> > Hello,
> > I have two Tables: Table1: GroupID1, GroupName1 and Table2:GroupID2,
> > GroupName2.
> > I have only one dropdown list (DropDownList1) in my application and
> through
> > a stored procedure I want to populate my DropDownList1 from GroupName1 and
> > GroupName2. How should I write my query?
> > Thanks,
> > Jim.
>

Saturday, March 24, 2012

Populate dropdown list

How can I populate a dropdownlist with data from a database using
Microsoft.ApplicationBlocks.Data ?
ThanksUse one of the SqlHelper methods. For instance, SqlHelper.ExecuteDataSet(.
. .), and then just map the DataSource = returnedDataSet.Tables[0];, and
then you can set which field for the text and the value. All the
applications blocks do is encapsulate the ado.net call, so instead of
creating your connection object, then command object and executing the
command to return something, you call a static method of SqlHelper passing
in a connection string or connection or transaction object, the query or
stored procedure to run, some other potential options, and you'll get a
result back.
I have wrapped the Data Access application block in my own class that, upon
instantiation, creates a connection from the connection string stored in my
config file, and then it's ready to exectue anything. On Dispose() of my
object, I close my connection.
Best regards,
Jeffrey Palermo
"Mark Goldin" <markgoldin@.comcast.net> wrote in message
news:ug6v1VccEHA.1764@.TK2MSFTNGP10.phx.gbl...
> How can I populate a dropdownlist with data from a database using
> Microsoft.ApplicationBlocks.Data ?
> Thanks
>
>
Mark,
If you look at the example in the Help for DropDownList there is one example
how to create a DropDownList through databinding.
Let us borrow some code from the example:
// Specify the data source and field names for the Text
// and Value properties of the items (ListItem objects)
// in the DropDownList control.
ColorList.DataSource = CreateDataSource();
ColorList.DataTextField = "ColorTextField";
ColorList.DataValueField = "ColorValueField";
..
But what you are interested in is how get the data from MS Data Access Block
right? Then declare the "CreateDataSource();" to return a ICollection just
like in the example and in then use SqlHelper class one of the 2 ways:
1: This one uses the dataset - not best in perfomance but easy to start with
DataSet ds = SqlHelper.ExecuteDataset(myConnection, "MyStoredProc", myParams
);
DataView dv = new DataView(ds.Tables["myTable"]);
return dv;
2: Use DataReader - a bit more code but better performance
// Call ExecuteReader static method of SqlHelper class that returns a SqlDat
aReader
SqlDataReader dr = SqlHelper.ExecuteReader connection, "MyStoredProc", myPa
rams);
if ((dr != null) && (dr.HasRows))
{
//Create Your Table here like in the example
..
while (dr.Read())
{
//Create a New DataRow here and assign values for each row in the DataReader
...
}
DataView dv = new DataView(myTable);
return dv;
}
Something like this!
Good Luck!
Thomas Andersson

Populate dropdown list

How can I populate a dropdownlist with data from a database using
Microsoft.ApplicationBlocks.Data ?
ThanksUse one of the SqlHelper methods. For instance, SqlHelper.ExecuteDataSet(.
.. .), and then just map the DataSource = returnedDataSet.Tables[0];, and
then you can set which field for the text and the value. All the
applications blocks do is encapsulate the ado.net call, so instead of
creating your connection object, then command object and executing the
command to return something, you call a static method of SqlHelper passing
in a connection string or connection or transaction object, the query or
stored procedure to run, some other potential options, and you'll get a
result back.

I have wrapped the Data Access application block in my own class that, upon
instantiation, creates a connection from the connection string stored in my
config file, and then it's ready to exectue anything. On Dispose() of my
object, I close my connection.

Best regards,
Jeffrey Palermo

"Mark Goldin" <markgoldin@.comcast.net> wrote in message
news:ug6v1VccEHA.1764@.TK2MSFTNGP10.phx.gbl...
> How can I populate a dropdownlist with data from a database using
> Microsoft.ApplicationBlocks.Data ?
> Thanks
>

Populate Dropdown from SQL Server

Hello everyone!
I'm trying to populate a dropdown list from SQL Server but it's not
being populated. I've tried all sorts of suggestions and examples that
I found from Google but I just can not get it to work. I am able to
access the database from other pages and display a list of records, but
I just can't get the data to go into the dd list.
Below is my code:
ASPX
<%@dotnet.itags.org. Page Language="VB" AutoEventWireup="false"
CodeFile="addApplication.aspx.vb" Inherits="addApplication" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlAgency" runat="server"
DataTextField="agName" DataValueField="agID" />
</div>
</form>
</body>
</html>
VB (code behind)
Partial Class addApplication
Inherits System.Web.UI.Page
Public Sub Page_click(ByVal sender As Object, ByVal e As EventArgs)
'Create a connection string
Dim conStr As String =
ConfigurationManager.ConnectionStrings("connString").ConnectionString
' connString is defined in web.config and works on other pages within
the website project
'Open a connection
Dim objConnection As OleDbConnection
objConnection = New OleDbConnection(conStr)
objConnection.Open()
'Specify the SQL string
Dim strSQL As String = "SELECT * FROM tblAgency;"
'Create a command object
Dim objCommand As OleDbCommand
objCommand = New OleDbCommand(strSQL, objConnection)
'Get a datareader
Dim objDataReader As OleDbDataReader
objDataReader =
objCommand.ExecuteReader(CommandBehavior.CloseConnection)
ddlAgency.DataSource = objDataReader
ddlAgency.DataBind()
'Close the datareader/db connection
objDataReader.Close()
End Sub
End Class
Is there something I'm doing wrong here?
thanks,
JerryWith Me.ddlSendDepartment
.DataSource = tblScreen
.DataTextField = "DepartmentName"
.DataValueField = "DepartmentID"
. DataBind()
End With
"Jerry" <jerryalan@.gmail.com> wrote in message
news:1147987219.226284.39470@.j55g2000cwa.googlegroups.com...
> Hello everyone!
> I'm trying to populate a dropdown list from SQL Server but it's not
> being populated. I've tried all sorts of suggestions and examples that
> I found from Google but I just can not get it to work. I am able to
> access the database from other pages and display a list of records, but
> I just can't get the data to go into the dd list.
> Below is my code:
> ASPX
> <%@. Page Language="VB" AutoEventWireup="false"
> CodeFile="addApplication.aspx.vb" Inherits="addApplication" %>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml" >
> <head runat="server">
> <title>Untitled Page</title>
> </head>
> <body>
> <form id="form1" runat="server">
> <div>
> <asp:DropDownList ID="ddlAgency" runat="server"
> DataTextField="agName" DataValueField="agID" />
> </div>
> </form>
> </body>
> </html>
> VB (code behind)
> Partial Class addApplication
> Inherits System.Web.UI.Page
> Public Sub Page_click(ByVal sender As Object, ByVal e As EventArgs)
> 'Create a connection string
> Dim conStr As String =
> ConfigurationManager.ConnectionStrings("connString").ConnectionString
> ' connString is defined in web.config and works on other pages within
> the website project
> 'Open a connection
> Dim objConnection As OleDbConnection
> objConnection = New OleDbConnection(conStr)
> objConnection.Open()
> 'Specify the SQL string
> Dim strSQL As String = "SELECT * FROM tblAgency;"
> 'Create a command object
> Dim objCommand As OleDbCommand
> objCommand = New OleDbCommand(strSQL, objConnection)
> 'Get a datareader
> Dim objDataReader As OleDbDataReader
> objDataReader =
> objCommand.ExecuteReader(CommandBehavior.CloseConnection)
> ddlAgency.DataSource = objDataReader
> ddlAgency.DataBind()
> 'Close the datareader/db connection
> objDataReader.Close()
> End Sub
> End Class
> Is there something I'm doing wrong here?
> thanks,
> --
> Jerry
>
Thanks for the reply Jeff. I tried your suggestion but I wasn't
successfull.
With Me.ddlAgency
.DataSource = objDataReader
.DataTextField = "agName"
.DataValueField = "agID"
.DataBind()
End With
'ddlAgency.DataSource = objDataReader
'ddlAgency.DataBind()
Did I do it wrong?
Jerry
Doh! I caught the mistake. I had the code in the click event handler
and not the page load handler.
Thanks Jeff, your suggestion does work.
Jerry

Populate Dropdown from SQL Server

Hello everyone!

I'm trying to populate a dropdown list from SQL Server but it's not
being populated. I've tried all sorts of suggestions and examples that
I found from Google but I just can not get it to work. I am able to
access the database from other pages and display a list of records, but
I just can't get the data to go into the dd list.

Below is my code:

ASPX
<%@dotnet.itags.org. Page Language="VB" AutoEventWireup="false"
CodeFile="addApplication.aspx.vb" Inherits="addApplication" %
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlAgency" runat="server"
DataTextField="agName" DataValueField="agID" />
</div>
</form>
</body>
</html
VB (code behind)

Partial Class addApplication
Inherits System.Web.UI.Page

Public Sub Page_click(ByVal sender As Object, ByVal e As EventArgs)
'Create a connection string

Dim conStr As String =
ConfigurationManager.ConnectionStrings("connString").ConnectionString

' connString is defined in web.config and works on other pages within
the website project

'Open a connection
Dim objConnection As OleDbConnection
objConnection = New OleDbConnection(conStr)
objConnection.Open()

'Specify the SQL string
Dim strSQL As String = "SELECT * FROM tblAgency;"

'Create a command object
Dim objCommand As OleDbCommand
objCommand = New OleDbCommand(strSQL, objConnection)

'Get a datareader
Dim objDataReader As OleDbDataReader
objDataReader =
objCommand.ExecuteReader(CommandBehavior.CloseConn ection)

ddlAgency.DataSource = objDataReader
ddlAgency.DataBind()

'Close the datareader/db connection
objDataReader.Close()
End Sub
End Class

Is there something I'm doing wrong here?

thanks,

--
JerryWith Me.ddlSendDepartment

.DataSource = tblScreen

.DataTextField = "DepartmentName"

.DataValueField = "DepartmentID"

.. DataBind()

End With

"Jerry" <jerryalan@.gmail.com> wrote in message
news:1147987219.226284.39470@.j55g2000cwa.googlegro ups.com...
> Hello everyone!
> I'm trying to populate a dropdown list from SQL Server but it's not
> being populated. I've tried all sorts of suggestions and examples that
> I found from Google but I just can not get it to work. I am able to
> access the database from other pages and display a list of records, but
> I just can't get the data to go into the dd list.
> Below is my code:
> ASPX
> <%@. Page Language="VB" AutoEventWireup="false"
> CodeFile="addApplication.aspx.vb" Inherits="addApplication" %>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml" >
> <head runat="server">
> <title>Untitled Page</title>
> </head>
> <body>
> <form id="form1" runat="server">
> <div>
> <asp:DropDownList ID="ddlAgency" runat="server"
> DataTextField="agName" DataValueField="agID" />
> </div>
> </form>
> </body>
> </html>
> VB (code behind)
> Partial Class addApplication
> Inherits System.Web.UI.Page
> Public Sub Page_click(ByVal sender As Object, ByVal e As EventArgs)
> 'Create a connection string
> Dim conStr As String =
> ConfigurationManager.ConnectionStrings("connString").ConnectionString
> ' connString is defined in web.config and works on other pages within
> the website project
> 'Open a connection
> Dim objConnection As OleDbConnection
> objConnection = New OleDbConnection(conStr)
> objConnection.Open()
> 'Specify the SQL string
> Dim strSQL As String = "SELECT * FROM tblAgency;"
> 'Create a command object
> Dim objCommand As OleDbCommand
> objCommand = New OleDbCommand(strSQL, objConnection)
> 'Get a datareader
> Dim objDataReader As OleDbDataReader
> objDataReader =
> objCommand.ExecuteReader(CommandBehavior.CloseConn ection)
> ddlAgency.DataSource = objDataReader
> ddlAgency.DataBind()
> 'Close the datareader/db connection
> objDataReader.Close()
> End Sub
> End Class
> Is there something I'm doing wrong here?
> thanks,
> --
> Jerry
Thanks for the reply Jeff. I tried your suggestion but I wasn't
successfull.

With Me.ddlAgency
.DataSource = objDataReader
.DataTextField = "agName"
.DataValueField = "agID"
.DataBind()
End With

'ddlAgency.DataSource = objDataReader
'ddlAgency.DataBind()

Did I do it wrong?

--
Jerry
Doh! I caught the mistake. I had the code in the click event handler
and not the page load handler.

Thanks Jeff, your suggestion does work.

--
Jerry

Populate dropdown

Hy Guys,

I have a datagrid that, in the edititem mode, have a dropdownlist. This
dropdownlist have to be populated with the items of another dropdownlist
that is in the same page.

How can I do it??

Thank'sthis is what you need

http://msdn.microsoft.com/library/d...stomcolumns.asp

--
Regards,

HD

"Andr Almeida Maldonado" <deweb@.bol.com.br> wrote in message
news:ebVFaqi2DHA.2428@.tk2msftngp13.phx.gbl...
> Hy Guys,
> I have a datagrid that, in the edititem mode, have a dropdownlist. This
> dropdownlist have to be populated with the items of another dropdownlist
> that is in the same page.
> How can I do it??
> Thank's

populate dropdown onchange without reloading page

I have 2 dropdowns, county and district.

When the county changes, I want the district dropdown to automatically
update with a list of districts in that county, but I don't want the
page to have to reload when county changes.

I remember doing this before in classic ASP with javascript, can I do
this with a dropdownlist in ASP.NET or should I stick with the HTML
dropdown?

Thanks,
Peter
--

"I hear ma train a comin'
... hear freedom comin"It has to be done in JavaScript. Whether you use an ASP.Net dropdownlist or
the HTML <select> really doesn't matter. Just note that items added to the
dropdownlist dynamically won't exist when you do post back. You'll need to
use the good-old Request.Form.

You can check out:
http://msdn.microsoft.com/library/d...tSpicedAjax.asp

which has an AJAX implementation of creating linked dropdowns..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
http://openmymind.net/redirector.aspx?documentId=51 - Learn about AJAX!

"Stimp" <ren@.spumco.com> wrote in message
news:slrndkl22h.k78.ren@.carbon.redbrick.dcu.ie...
>I have 2 dropdowns, county and district.
> When the county changes, I want the district dropdown to automatically
> update with a list of districts in that county, but I don't want the
> page to have to reload when county changes.
> I remember doing this before in classic ASP with javascript, can I do
> this with a dropdownlist in ASP.NET or should I stick with the HTML
> dropdown?
> Thanks,
> Peter
> --
> "I hear ma train a comin'
> ... hear freedom comin"

Populate dropdown list with XML

How can I fill a dropdown list with XML data. Simplest way please. I would like to user server.mappath to get the data from the file. Is it possible to use a recordset

I need to populate the ID and the value of the record.

Code sample appreciated...thanksPlease see below code, that helps for your question.

Here is the simple XML file, I am using to bind to dropdownlist and name is as Xmlfile.xml


<?xml version="1.0" encoding="utf-8" ?>
<DataList>
<Student>
<Name>Creator</Name >
<Roll>101</Roll >
</Student >
<Student>
<Name>Maintainer</Name >
<Roll>102</Roll >
</Student
</DataList>

and I am using this file to bind with Dropdownlost in page load event. Assuming DropDownList server control name is DropDownList1


Dim Ds As New System.Data.DataSet
Ds.ReadXml("D:\WebSites\VS2005First\XMLFile.xml")

DropDownList1.DataSource = Ds.Tables(0).DefaultView
DropDownList1.DataTextField = "Roll"
DropDownList1.DataValueField = "Name"
DropDownList1.DataBind()

Hope this helps you!
ok that is pretty easy,fist of all u need to populate ur xml data to your datagrid then bind it to ur dropdownlist.

xml file
<root>
<userName>Bob</userName>
<userID>1</userID>
</root
vb.net
'read your data from your datagrid
'assuming ur xml is your root directory
'

Dim ds As DataSet
ds.ReadXml(Server.MapPath("file.xml"))
'bind your dataset to your dropdown list
DropDownList1.DataSource = ds
'assign the element of ur xml to the ddl
DropDownList1.DataTextField = "userName"
DropDownList1.DataValueField = "userID"
DropDownList1.DataBind()

hope that help.Happy coding
Thank you all for replying - I will try the suggestions given

Populate Dropdown with File Names

I'm working with an ASP.Net web page that's using C#. I'm trying to
populate a Dropdown with the File Names that reside in an image folder
that's in the same project. So far all I've been able to find is the
below code...and even then that's not quite what I'm after.

DirectoryInfo di = new DirectoryInfo(@dotnet.itags.org."~/Images/Entry/");
FileInfo[] fis = di.GetFiles();
DropDownList5.DataSource = fis;
DropDownList5.DataTextField = "Name";
DropDownList5.DataValueField = "Name";
DropDownList5.DataBind();

Any help in trying to get this resolved quickly would be appreciated.

ThanksOne thing I neglected to say...the Dropdown will also reside in the
EditItemTemplate of a Formview. Is there a way to bind that control if
it's there?

Thanks Again

Dave wrote:

Quote:

Originally Posted by

I'm working with an ASP.Net web page that's using C#. I'm trying to
populate a Dropdown with the File Names that reside in an image folder
that's in the same project. So far all I've been able to find is the
below code...and even then that's not quite what I'm after.
>
DirectoryInfo di = new DirectoryInfo(@."~/Images/Entry/");
FileInfo[] fis = di.GetFiles();
DropDownList5.DataSource = fis;
DropDownList5.DataTextField = "Name";
DropDownList5.DataValueField = "Name";
DropDownList5.DataBind();
>
Any help in trying to get this resolved quickly would be appreciated.
>
Thanks

Populate DropDown with dataview

I have a bunch of dropdown on my form and I am trying to optimize the populating process. I am filling a dataview with a store proc and pass the dataview to each dropdown's datasource with a filter and sort expression. Until then every thing is fine however I need to add a black row at the top of each drop down and I dont know how to add it to my dataview or directly to dropdown. Any help would be great.

Here is what I got up to now


fctFillDropDownList(ddSource, 30, strSortAsc, dvTemp)
...

'*************************************************************************
'*************************************************************************
Private Sub fctFillDropDownList(ByRef objControl As System.Web.UI.WebControls.DropDownList, _
ByVal iParentID As Int16, ByVal strSort As String, ByRef dvTemp As DataView)
'*************************************************************************
dvTemp.RowFilter = "parentId=" & iParentID
dvTemp.Sort = strSort
objControl.DataSource = dvTemp
objControl.DataTextField = "Texte"
objControl.DataValueField = "Texte"
End Sub

After you call DataBind on the dropdownlist, you can do this (C# syntax):

MyDropDownList.Items.Insert(0, new ListItem("", ""));

That will add a blank row at the beginning of the dropdownlist.

HTH
Thanks that worked