Showing posts with label populated. Show all posts
Showing posts with label populated. Show all posts

Monday, March 26, 2012

populate a dg column based upon the value populated to another row in the datagrid

Is it possible when populating a datagrid to populate a dropdownlist
column based upon the value populated to another row in the datagrid?
(i.e. I have a drop down which I want to populate differently for each
row with user names based upon a UserRegionID which is also a column in
the row).
Since you don't actually know the value of the UserRegionID until the
data is already bound to the datagrid, how do you do this?
Thanks,
Mike
*** Sent via Developersdex http://www.examnotes.net ***You can use the datagrid events (RowDataBound I think) to handle this
by populating your dropdown using the id of each row as the grid is
populated
HTH
---
David Gray
ASP.NET/C# Developer/Architect (MCAD.NET)
On Fri, 13 Jan 2006 02:23:16 -0800, Mike P <mike.parr@.gmail.com>
wrote:

>Is it possible when populating a datagrid to populate a dropdownlist
>column based upon the value populated to another row in the datagrid?
>(i.e. I have a drop down which I want to populate differently for each
>row with user names based upon a UserRegionID which is also a column in
>the row).
>Since you don't actually know the value of the UserRegionID until the
>data is already bound to the datagrid, how do you do this?
>
>Thanks,
>Mike
>
>*** Sent via Developersdex http://www.examnotes.net ***

populate a dg column based upon the value populated to another row in the datagrid

Is it possible when populating a datagrid to populate a dropdownlist
column based upon the value populated to another row in the datagrid?
(i.e. I have a drop down which I want to populate differently for each
row with user names based upon a UserRegionID which is also a column in
the row).

Since you don't actually know the value of the UserRegionID until the
data is already bound to the datagrid, how do you do this?

Thanks,

Mike

*** Sent via Developersdex http://www.developersdex.com ***You can use the datagrid events (RowDataBound I think) to handle this
by populating your dropdown using the id of each row as the grid is
populated

HTH

-------------
David Gray
ASP.NET/C# Developer/Architect (MCAD.NET)

On Fri, 13 Jan 2006 02:23:16 -0800, Mike P <mike.parr@.gmail.com>
wrote:

>Is it possible when populating a datagrid to populate a dropdownlist
>column based upon the value populated to another row in the datagrid?
>(i.e. I have a drop down which I want to populate differently for each
>row with user names based upon a UserRegionID which is also a column in
>the row).
>Since you don't actually know the value of the UserRegionID until the
>data is already bound to the datagrid, how do you do this?
>
>Thanks,
>Mike
>
>*** Sent via Developersdex http://www.developersdex.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 controls once

How do I make sure that when an execution is taken back to the server
controls like combos are not populated again?
ThanksIf you populate your controls in the Page_Load event, you can wrap the code
that populates the controls in an
If Not Page.isPostBack
...
End If
block. This will only execute the code (within the block) once; when the pag
e loads initially. You can ensure that the data remains in the controls by s
etting the controls viewstate property to true.
hope this helps,
John
"Mark Goldin" wrote:

> How do I make sure that when an execution is taken back to the server
> controls like combos are not populated again?
> Thanks
>
>
Thanks, that worked.
"John Sivilla" <JohnSivilla@.discussions.microsoft.com> wrote in message
news:559371D8-5400-46F4-A256-2D3EF6C0BD70@.microsoft.com...
> If you populate your controls in the Page_Load event, you can wrap the
code that populates the controls in an
> If Not Page.isPostBack
> ...
> End If
> block. This will only execute the code (within the block) once; when the
page loads initially. You can ensure that the data remains in the controls
by setting the controls viewstate property to true.
> hope this helps,
> John
> "Mark Goldin" wrote:
>

Populate controls once

How do I make sure that when an execution is taken back to the server
controls like combos are not populated again?

ThanksIf you populate your controls in the Page_Load event, you can wrap the code that populates the controls in an

If Not Page.isPostBack
...
End If

block. This will only execute the code (within the block) once; when the page loads initially. You can ensure that the data remains in the controls by setting the controls viewstate property to true.

hope this helps,
John

"Mark Goldin" wrote:

> How do I make sure that when an execution is taken back to the server
> controls like combos are not populated again?
> Thanks
>
Thanks, that worked.

"John Sivilla" <JohnSivilla@.discussions.microsoft.com> wrote in message
news:559371D8-5400-46F4-A256-2D3EF6C0BD70@.microsoft.com...
> If you populate your controls in the Page_Load event, you can wrap the
code that populates the controls in an
> If Not Page.isPostBack
> ...
> End If
> block. This will only execute the code (within the block) once; when the
page loads initially. You can ensure that the data remains in the controls
by setting the controls viewstate property to true.
> hope this helps,
> John
> "Mark Goldin" wrote:
> > How do I make sure that when an execution is taken back to the server
> > controls like combos are not populated again?
> > Thanks

Saturday, March 24, 2012

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 DropDownList from DB

Hi,
Could someone tell me how can be populated DropdownList from Data Base. I
use ASP.NET/C#/MS SQL SERVER.
Thanks!
Viktor
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.721 / Virus Database: 477 - Release Date: 16.7.2004 a.Victor,
You need to get the database data into a dataset, set the DropdownList
properties DataSource to the dataset name and DataMember to the table name
and call DataBind method.
Eliyahu
"Viktor Popov" <viketo@.yahoo.com> wrote in message
news:OMEUyaGdEHA.212@.TK2MSFTNGP12.phx.gbl...
> Hi,
> Could someone tell me how can be populated DropdownList from Data Base. I
> use ASP.NET/C#/MS SQL SERVER.
> Thanks!
> Viktor
>
> --
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.721 / Virus Database: 477 - Release Date: 16.7.2004 a.
>
Thanks for the reply!
Could you add the other code?
SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");
SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer ",
conn);
DataSet ds = new DataSet();
da.Fill (ds,"Table");
DropDownList2.DataSource = ds;
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
DropDownList2.DataMember = "Table";
DataBind (); // or DropDownList2.DataBind ();
"Viktor Popov" <viketo@.yahoo.com> wrote in message
news:%23QdEA%23HdEHA.2664@.TK2MSFTNGP09.phx.gbl...
> Thanks for the reply!
> Could you add the other code?
> SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
> Catalog=Estate; User ID=blek; Password=banderas");
> SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer ",
> conn);
>
> DataSet ds = new DataSet();
> da.Fill (ds,"Table");
> DropDownList2.DataSource = ds;
>
>
> --
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
>
>
Hi, Thanks again for the reply! I wrote that but it doesn't work. Could you
check for an error?
Thanks
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");
SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer ",
conn);
DataSet ds = new DataSet();
da.Fill (ds,"Table");
DropDownList2.DataSource = ds;
DropDownList2.DataMember = "Table";
DropDownList2.DataBind () ;
}
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
see inline
"Viktor Popov" <viketo@.yahoo.com> wrote in message news:O5PJdnIdEHA.3616@.TK2MSFTNGP10.phx.g
bl...
> Hi, Thanks again for the reply! I wrote that but it doesn't work. Could yo
u
> check for an error?
> Thanks
> private void Page_Load(object sender, System.EventArgs e)
> {
> SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
> Catalog=Estate; User ID=blek; Password=banderas");
> SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer ",
> conn);
>
> DataSet ds = new DataSet();
> da.Fill (ds,"Table");
> DropDownList2.DataSource = ds;
> DropDownList2.DataMember = "Table";
> DropDownList2.DataBind () ;
>
add the code (with the correct values):
DropDownList2.DataTextField = "<name of column for visible text>";
DropDownList2.DataValueField = "<name of column for internal value>";

> }
>
> --
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
>
I tryed to do that:
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");
SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer ",
conn);
conn.Open();
da.SelectCommand.ExecuteReader();
DataSet ds = new DataSet();
da.Fill (ds,"Table");
DropDownList2.DataSource = ds;
DropDownList2.DataMember = "Table";
DropDownList2.DataTextField = "TypeOffer";
DropDownList2.DataValueField = "OfferID";
DropDownList2.DataBind () ;]
}
but it doesn't work.
What could I do?
Thanks!
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
what's the error?
"Viktor Popov" <viketo@.yahoo.com> wrote in message
news:OHTu$fJdEHA.1384@.TK2MSFTNGP10.phx.gbl...
> I tryed to do that:
> private void Page_Load(object sender, System.EventArgs e)
> {
> // Put user code to initialize the page here
> SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
> Catalog=Estate; User ID=blek; Password=banderas");
> SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer
",
> conn);
> conn.Open();
> da.SelectCommand.ExecuteReader();
> DataSet ds = new DataSet();
> da.Fill (ds,"Table");
> DropDownList2.DataSource = ds;
> DropDownList2.DataMember = "Table";
> DropDownList2.DataTextField = "TypeOffer";
> DropDownList2.DataValueField = "OfferID";
> DropDownList2.DataBind () ;]
> }
> but it doesn't work.
> What could I do?
> Thanks!
>
> --
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
>
There is no error. When I start the application the DropDownList is empty.
There are no values, no items. My table is full with data
TypeOffer
--
OfferID TypeOffer
1 rent
2 sell
3 change
Do you know what's wrong?
"Eliyahu Goldin" <removemeegoldin@.monarchmed.com> wrote in message
news:eahN5iJdEHA.2664@.TK2MSFTNGP09.phx.gbl...
> what's the error?
> "Viktor Popov" <viketo@.yahoo.com> wrote in message
> news:OHTu$fJdEHA.1384@.TK2MSFTNGP10.phx.gbl...
blek.TypeOffer
> ",
>
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
Can set a breakpoint after the line with da.Fill (ds,"Table"); and check if
the dataset actually gets any data?
"Viktor Popov" <viketo@.yahoo.com> wrote in message
news:%231PE14JdEHA.2544@.TK2MSFTNGP10.phx.gbl...
> There is no error. When I start the application the DropDownList is empty.
> There are no values, no items. My table is full with data
> TypeOffer
> --
> OfferID TypeOffer
> 1 rent
> 2 sell
> 3 change
> Do you know what's wrong?
> "Eliyahu Goldin" <removemeegoldin@.monarchmed.com> wrote in message
> news:eahN5iJdEHA.2664@.TK2MSFTNGP09.phx.gbl...
> blek.TypeOffer
>
> --
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
>

populate DropDownList from DB

Hi,
Could someone tell me how can be populated DropdownList from Data Base. I
use ASP.NET/C#/MS SQL SERVER.

Thanks!

Viktor

--
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.721 / Virus Database: 477 - Release Date: 16.7.2004 a.Victor,

You need to get the database data into a dataset, set the DropdownList
properties DataSource to the dataset name and DataMember to the table name
and call DataBind method.

Eliyahu

"Viktor Popov" <viketo@.yahoo.com> wrote in message
news:OMEUyaGdEHA.212@.TK2MSFTNGP12.phx.gbl...
> Hi,
> Could someone tell me how can be populated DropdownList from Data Base. I
> use ASP.NET/C#/MS SQL SERVER.
> Thanks!
> Viktor
>
> --
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.721 / Virus Database: 477 - Release Date: 16.7.2004 a.
Thanks for the reply!

Could you add the other code?

SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");

SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer ",
conn);

DataSet ds = new DataSet();

da.Fill (ds,"Table");

DropDownList2.DataSource = ds;

--
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
DropDownList2.DataMember = "Table";
DataBind (); // or DropDownList2.DataBind ();

"Viktor Popov" <viketo@.yahoo.com> wrote in message
news:%23QdEA%23HdEHA.2664@.TK2MSFTNGP09.phx.gbl...
> Thanks for the reply!
> Could you add the other code?
> SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
> Catalog=Estate; User ID=blek; Password=banderas");
> SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer ",
> conn);
>
> DataSet ds = new DataSet();
> da.Fill (ds,"Table");
> DropDownList2.DataSource = ds;
>
>
> --
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
Hi, Thanks again for the reply! I wrote that but it doesn't work. Could you
check for an error?

Thanks

private void Page_Load(object sender, System.EventArgs e)

{

SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");

SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer ",
conn);

DataSet ds = new DataSet();

da.Fill (ds,"Table");

DropDownList2.DataSource = ds;

DropDownList2.DataMember = "Table";

DropDownList2.DataBind () ;

}

--
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
see inline

"Viktor Popov" <viketo@.yahoo.com> wrote in message news:O5PJdnIdEHA.3616@.TK2MSFTNGP10.phx.gbl...
> Hi, Thanks again for the reply! I wrote that but it doesn't work. Could you
> check for an error?
> Thanks
> private void Page_Load(object sender, System.EventArgs e)
> {
> SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
> Catalog=Estate; User ID=blek; Password=banderas");
> SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer ",
> conn);
>
> DataSet ds = new DataSet();
> da.Fill (ds,"Table");
> DropDownList2.DataSource = ds;
> DropDownList2.DataMember = "Table";
> DropDownList2.DataBind () ;

add the code (with the correct values):
DropDownList2.DataTextField = "<name of column for visible text>";
DropDownList2.DataValueField = "<name of column for internal value>";

> }
>
> --
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
I tryed to do that:
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");
SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer ",
conn);
conn.Open();
da.SelectCommand.ExecuteReader();
DataSet ds = new DataSet();
da.Fill (ds,"Table");
DropDownList2.DataSource = ds;
DropDownList2.DataMember = "Table";
DropDownList2.DataTextField = "TypeOffer";
DropDownList2.DataValueField = "OfferID";
DropDownList2.DataBind () ;]
}

but it doesn't work.
What could I do?

Thanks!

--
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
what's the error?

"Viktor Popov" <viketo@.yahoo.com> wrote in message
news:OHTu$fJdEHA.1384@.TK2MSFTNGP10.phx.gbl...
> I tryed to do that:
> private void Page_Load(object sender, System.EventArgs e)
> {
> // Put user code to initialize the page here
> SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
> Catalog=Estate; User ID=blek; Password=banderas");
> SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer
",
> conn);
> conn.Open();
> da.SelectCommand.ExecuteReader();
> DataSet ds = new DataSet();
> da.Fill (ds,"Table");
> DropDownList2.DataSource = ds;
> DropDownList2.DataMember = "Table";
> DropDownList2.DataTextField = "TypeOffer";
> DropDownList2.DataValueField = "OfferID";
> DropDownList2.DataBind () ;]
> }
> but it doesn't work.
> What could I do?
> Thanks!
>
> --
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
There is no error. When I start the application the DropDownList is empty.
There are no values, no items. My table is full with data
TypeOffer
-------
OfferID TypeOffer
1 rent
2 sell
3 change

Do you know what's wrong?
"Eliyahu Goldin" <removemeegoldin@.monarchmed.com> wrote in message
news:eahN5iJdEHA.2664@.TK2MSFTNGP09.phx.gbl...
> what's the error?
> "Viktor Popov" <viketo@.yahoo.com> wrote in message
> news:OHTu$fJdEHA.1384@.TK2MSFTNGP10.phx.gbl...
> > I tryed to do that:
> > private void Page_Load(object sender, System.EventArgs e)
> > {
> > // Put user code to initialize the page here
> > SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
> > Catalog=Estate; User ID=blek; Password=banderas");
> > SqlDataAdapter da = new SqlDataAdapter ("Select * FROM
blek.TypeOffer
> ",
> > conn);
> > conn.Open();
> > da.SelectCommand.ExecuteReader();
> > DataSet ds = new DataSet();
> > da.Fill (ds,"Table");
> > DropDownList2.DataSource = ds;
> > DropDownList2.DataMember = "Table";
> > DropDownList2.DataTextField = "TypeOffer";
> > DropDownList2.DataValueField = "OfferID";
> > DropDownList2.DataBind () ;]
> > }
> > but it doesn't work.
> > What could I do?
> > Thanks!
> > --
> > Outgoing mail is certified Virus Free.
> > Checked by AVG anti-virus system (http://www.grisoft.com).
> > Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.

--
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
Can set a breakpoint after the line with da.Fill (ds,"Table"); and check if
the dataset actually gets any data?

"Viktor Popov" <viketo@.yahoo.com> wrote in message
news:%231PE14JdEHA.2544@.TK2MSFTNGP10.phx.gbl...
> There is no error. When I start the application the DropDownList is empty.
> There are no values, no items. My table is full with data
> TypeOffer
> -------
> OfferID TypeOffer
> 1 rent
> 2 sell
> 3 change
> Do you know what's wrong?
> "Eliyahu Goldin" <removemeegoldin@.monarchmed.com> wrote in message
> news:eahN5iJdEHA.2664@.TK2MSFTNGP09.phx.gbl...
> > what's the error?
> > "Viktor Popov" <viketo@.yahoo.com> wrote in message
> > news:OHTu$fJdEHA.1384@.TK2MSFTNGP10.phx.gbl...
> > > I tryed to do that:
> > > private void Page_Load(object sender, System.EventArgs e)
> > > {
> > > // Put user code to initialize the page here
> > > > SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
> > > Catalog=Estate; User ID=blek; Password=banderas");
> > > SqlDataAdapter da = new SqlDataAdapter ("Select * FROM
> blek.TypeOffer
> > ",
> > > conn);
> > > conn.Open();
> > > da.SelectCommand.ExecuteReader();
> > > DataSet ds = new DataSet();
> > > da.Fill (ds,"Table");
> > > DropDownList2.DataSource = ds;
> > > DropDownList2.DataMember = "Table";
> > > DropDownList2.DataTextField = "TypeOffer";
> > > DropDownList2.DataValueField = "OfferID";
> > > DropDownList2.DataBind () ;]
> > > }
> > > > but it doesn't work.
> > > What could I do?
> > > > Thanks!
> > > > > --
> > > Outgoing mail is certified Virus Free.
> > > Checked by AVG anti-virus system (http://www.grisoft.com).
> > > Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
> > >
> --
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
It doesn't work...I'm stucked....
My last code is this. When I RUN it there are no errors but the DropDownList
is not populated and the msg.Text=""

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");
SqlDataAdapter dad = new SqlDataAdapter ("SELECT blek.TypeOffer.OfferID,
blek.TypeOffer.TypeOffer FROM blek.TypeOffer", conn);

DataTable table=new DataTable();
conn.Open();
dad.Fill(table);
conn.Close();
DropDownList2.DataSource=table;
DropDownList2.DataValueField = "OfferID";
DropDownList2.DataTextField = "TypeOffer";
DropDownList2.DataBind ();
msg.Text = table.Rows.Count.ToString();
}

--
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
DropDownList2.DataSource=table;

try: DropDownList2.DataSource=table.DefaultView;

"Viktor Popov" <viketo@.yahoo.com> wrote in message
news:uAGYoqKdEHA.1692@.tk2msftngp13.phx.gbl...
> It doesn't work...I'm stucked....
> My last code is this. When I RUN it there are no errors but the
DropDownList
> is not populated and the msg.Text=""
>
> private void Page_Load(object sender, System.EventArgs e)
> {
> // Put user code to initialize the page here
> SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
> Catalog=Estate; User ID=blek; Password=banderas");
> SqlDataAdapter dad = new SqlDataAdapter ("SELECT
blek.TypeOffer.OfferID,
> blek.TypeOffer.TypeOffer FROM blek.TypeOffer", conn);
>
> DataTable table=new DataTable();
> conn.Open();
> dad.Fill(table);
> conn.Close();
> DropDownList2.DataSource=table;
> DropDownList2.DataValueField = "OfferID";
> DropDownList2.DataTextField = "TypeOffer";
> DropDownList2.DataBind ();
> msg.Text = table.Rows.Count.ToString();
> }
>
> --
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.

Wednesday, March 21, 2012

populated a dropdown with data in the rows, but populating a dropdown with names of the co

within my pageload i have
SqlConnection rate;
SqlCommand Selct;
SqlDataReader dbresults;
rate = new SqlConnection( @dotnet.itags.org."xoxo");
rate.Open();
Selct = new SqlCommand( "Select UserID From dbo.Users", rate );
dbresults = Selct.ExecuteReader();
DropDownList1.DataSource = dbresults;
DropDownList1.DataTextField = "UserID*";
DropDownList1.DataBind();
dbresults.Close();
rate.Close();try out

...
myda = New SqlClient.SqlDataAdapter("Select * from <tablename>" , myconnection)
ds = New DataSet()
myda.Fill(ds, "TableName")
Dim dc As DataColumn
For Each dc In ds.Tables(0).Columns
ddl.Items.Add(dc.ColumnName)
Next
.....

HTH
ok heres another one ive been trying to figure out , how do a populate a drop down list with each table name in a database

populating a datagrid from a parameterized procedure during page load

I have a datagrid control on my page populated from a parameterized stored procedure. The parameter comes from a label control on my page as seen below. I cannot get the grid to populate. Any suggestions?

Dim LocalIP = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If LocalIP =""Then
LocalIP = Request.ServerVariables("REMOTE_ADDR")
EndIf
Label1.Text = LocalIP

TIA <JP>

Hi paskettj,

first check if Label1.text has been assigned to the correct value(mean, what's the value of " LocalIP" ? you can use debug, or response.write to monitor it);

secondly,check if your sotred procedure has returned the correct data;

and the third, pls check if you have use datagrid correctly. To get your datagrid control populated with corresponding data,First, you need to set the correct DataSource or DataSourceID, then call the Databind() method. It doesn't matter where you get your data from(whether it's from a table,backend database, from parameterized stored procedure...etc).

for more detailed information, i suggest you reading some material by clicking the following links:

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.basedatalist.datasource(VS.71).aspx (the sample within this page should help u use datagrid)

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.aspx (to a overview of datagird class)

hope my suggestion helps :)

Populating a DataGrid with a Stored Procedure based on ddl values

Hello All -

First off, I am a complete newbie.

I have created a page with 5 dropdownlists that I populated using stored procedures.

Question one:

How Do I display a default value for these Dropdowns?

I have configured a DataGrid using another Stored Procedure.

Question 2:

How Do I populate the DG with the values specified within the 5 dropdownlists using a stored procedure?

Here is a copy of the current aspx page.

<%@dotnet.itags.org.PageLanguage="VB"MasterPageFile="~/MasterPage.master"Title="CTM PreRunbook" %>

<asp:ContentID="Content1"ContentPlaceHolderID="ContentPlaceHolder1"Runat="Server">

<table><tr><thalign="center"colspan="3"style="vertical-align: middle; text-align: center"valign="middle">

<asp:LabelID="Label1"runat="server"Font-Size="Large"Text="Data Feed Query"></asp:Label> </th></tr>

<tr>

<th>

Application Name:</th>

<tdalign="right"style="vertical-align: top; text-align: left">

<tdstyle="width: 31px">

<asp:DropDownListID="ddlAppName"runat="server"AutoPostBack="True"DataSourceID="SqlDataSource1"DataTextField="AppName"DataValueField="AppName">

<asp:ListItem>- Select -</asp:ListItem>

</asp:DropDownList><asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:PreRunbookConnectionString %>"

SelectCommand="spGet_AppName"SelectCommandType="StoredProcedure"></asp:SqlDataSource>

</td></tr>

<tr>

<th>

Data Feed Name:</th>

<tdalign="right"style="vertical-align: top; text-align: left">

<tdstyle="width: 31px; text-align: left;">

<asp:DropDownListID="ddlDataFeedName"runat="server"AutoPostBack="True"DataSourceID="SqlDataSourceDependantFeeds"DataTextField="FeedName"DataValueField="FeedName">

</asp:DropDownList><asp:SqlDataSourceID="SqlDataSourceDependantFeeds"runat="server"

ConnectionString="<%$ ConnectionStrings:PreRunbookConnectionString %>"SelectCommand="spGet_FeedNames"

SelectCommandType="StoredProcedure"></asp:SqlDataSource>

</td></tr>

<tr>

<th>

Account:</th>

<tdalign="right"style="vertical-align: top; text-align: left">

<tdstyle="width: 31px">

<asp:DropDownListID="ddlAccount"runat="server"AutoPostBack="True"DataSourceID="SqlDataSourceServiceAccount"DataTextField="ServiceAccounts"DataValueField="ServiceAccounts">

</asp:DropDownList><asp:SqlDataSourceID="SqlDataSourceServiceAccount"runat="server"

ConnectionString="<%$ ConnectionStrings:PreRunbookConnectionString %>"SelectCommand="spGetDistinctServiceAccounts"

SelectCommandType="StoredProcedure"></asp:SqlDataSource>

</td></tr>

<tr>

<th>

Platform:</th>

<tdalign="right"style="vertical-align: top; text-align: left">

<tdstyle="width: 31px; text-align: left;">

<asp:DropDownListID="ddlPlatform"runat="server"AutoPostBack="True"DataSourceID="SqlDataSourcePlatform"DataTextField="Platform"DataValueField="Platform">

</asp:DropDownList><asp:SqlDataSourceID="SqlDataSourcePlatform"runat="server"ConnectionString="<%$ ConnectionStrings:PreRunbookConnectionString %>"

SelectCommand="spGet_Platform"SelectCommandType="StoredProcedure"></asp:SqlDataSource>

</td></tr>

<tr>

<th>

Scheduled Run Days:</th>

<tdalign="right"style="vertical-align: top; text-align: left">

<tdstyle="width: 31px">

<asp:DropDownListID="ddlSchedRunDays"runat="server"AutoPostBack="True"DataSourceID="SqlDataSourceSchedRunDays"DataTextField="ScheduledRunDays"DataValueField="ScheduledRunDays">

</asp:DropDownList><asp:SqlDataSourceID="SqlDataSourceSchedRunDays"runat="server"

ConnectionString="<%$ ConnectionStrings:PreRunbookConnectionString %>"SelectCommand="spGet_ScheduledRunDays"

SelectCommandType="StoredProcedure"></asp:SqlDataSource>

</td></tr>

</Table>

<table>

<tr>

<tdstyle="width: 100px"align="center">

<asp:ButtonID="btnFilter"runat="server"Text="Filter"/></td>

<tdstyle="width: 100px">

<asp:ButtonID="btnReset"runat="server"Text="Reset"/></td>

</tr>

<tr>

<tdcolspan="2"align="center">

<asp:ButtonID="btnShowAllDataFeeds"runat="server"Text="Show All Data Feeds"/></td>

</tr>

</table>

<br/>

<asp:GridViewID="GridView1"runat="server"AllowPaging="True"AllowSorting="True"

AutoGenerateColumns="False"DataSourceID="SqlDataSourceGridView">

</asp:GridView>

<asp:SqlDataSourceID="SqlDataSourceGridView"runat="server"ConnectionString="<%$ ConnectionStrings:PreRunbookConnectionString %>"

SelectCommand="spFilterByServAcct"SelectCommandType="StoredProcedure">

<SelectParameters>

<asp:FormParameterFormField="ServiceAccounts"Name="ServAcct"

Type="String"/>

</SelectParameters>

</asp:SqlDataSource>

<br>

</asp:Content>

Thanks ,

Pat

here is the best example that describes your needs

http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/sqldatasource.aspx

Populating a dropdown box

Hi all,

I have populated one dropdown menu using data from a database using the following code

Sub Page_Load (seander as Object, e as EventArgs) Year.DataSource = GetYear() Year.DataBind()End Sub Function GetYear() As System.Data.SqlClient.SqlDataReader Dim connectionString As String = "server='localhost'; user id='sa'; password='eoyson'; Database='PupilRoll'" Dim sqlConnection As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(connectionString) Dim queryString As String = "SELECT [Years].* FROM [Years]" Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection) sqlConnection.Open Dim dataReader As System.Data.SqlClient.SqlDataReader = sqlCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection) Return dataReader End Function
I am now trying to populate another dropdown list depending on the information selected from the previous dropdown list.
Cheers!

Try the tutorial right on this site:

Master Detail Filtering with Two Dropdownlists

Friday, March 16, 2012

populating a dropdownlist with data from a multidimensional arrays

Hello,

I hope someone can help me. I have an array defined as a two dimensional array that was populated from a dataset. I want to take that array and populate a dropdownlist. The reason I created a two dimensional array was because in the first dimension I store the value of the dropdownlist and in the second dimension I store the text for the dropdownlist. The value is not just a column from the dataset but a string that is pieced together. Please take a look at the code below and tell me what I'm doing wrong. If someone can please help me. I'm really lost.


'This is were I populate the array
Dim objArray(,) As Object

ReDim objArray(intRowCnt - 1, intColCnt - 1)
i = 0
For Each dr In mDSet.Tables(0).Rows
With mDSet.Tables.Item(0).Rows(i)
If .Item("mbo_key").ToString.Length > 0 Then
objArray(i, 0) = .Item(3).ToString.Trim & "|" & .Item(4).ToString.Trim
objArray(i, 1) = .Item(1).ToString.Trim & " - " & .Item(2).ToString.Trim & " *"
Else
objArray(i, 0) = .Item("period_key").ToString
objArray(i, 1) = .Item(1).ToString.Trim & " - " & .Item(2).ToString.Trim
End If
End With
i += 1
Next

With ddlDropDownList
.DataSource = objArray
For i = 0 To intMBO
' this is were it's blowing up!!!!!
.DataValueField = CStr(objArray(i, 0))
.DataTextField = CStr(objArray(i, 1))
Next
.DataBind()
End With

Why again are you not binding it straight to the Dataset? You should be able to do this using a Dataset, even if it is a concatenation of certain columns (you may even be able to this at the DB level)

As for your problem...you may be able to fix it if instead of using a multidimensial array, use a arrayed class that you define, then you should be able to set the .datavalue/textfield to the class properties (yes use properties). It would look much cleaner too. Very simple class

Public Class Items
Public Property Text...
Public Property Value...
End Class

Hope this helps!
--Michael
I thank you for your help but I'm not sure what you mean. I created 2 Properties like you suggested but I get an error saying I can't convert an array to string when I try to assign it to the .DataValueField of the dropdownlist. Can you please show me an example because I'm really fustrated with this problem.

Also the reason I don't bind it to a dataset is because the value of an option changes depending on if a certain field is Null or not. I wasn't sure how to do this.


With selPeriod
.DataValueField = mclsMBO.marstrValue
.DataTextField = mclsMBO.marstrText
End With

I want to thank you for your help. You set a light off in my head and I ran with it. What I did was I added to Columns to the dataset and populated them with the info I needed it and then I binded those fields to the drop down list. Once again I want to say thanks.

populating a list box by alphabetical surname

Hey guys

I have a listbox contianing strings which ahs been populated from a databsae via "SELECT * FROM researchers"

i.e

John Doe www.johndoe.com
David Allen www.daveallen.com
Andrew Rodgers www.andrewdavidson.com
..

I need this to be displayed as

David Allen www.daveallen.com
John Doe www.johndoe.com
Andrew Rodgers www.andrewdavidson.com

i.e alphabetically by surname?
How do I do this?SELECT * FROM researchers ORDER BY LastName ASC

Populating a listbox from DB w/o postback

Hi,

I have 2 listboxes. The first gets populated from the db as soon as the page loads. The second listbox get populated based on the user's selection from the first listbox. However, currently the code is such that with each selection there is a postback. We want to avoid it using filter and javascript. I am not using ADO.NET but adodbc and no datagrids or datasets (please don't tell me that i should as my boss clearly doesn't want to get into it at this stage).

How can i do it?

Thanks

Currently the code with the postback is as follows:
page_load

if not page.ispostback then
Do While Not rs.EOF
li = New ListItem(rs("cat_name").Value, rs("cat_id").Value)
List1.Items.Add(li)
rs.MoveNext()
Loop

End If

'List1.SelectedIndex = 0
rs.Close()
rs = Nothing

List1.SelectedValue = cat_id

'here there is some other code not relevant to the listboxes

'select items for second listbox
Dim rsSub As New ADODB.Recordset
rsSub.Open("SELECT sub_name, sub_id FROM subs WHERE cat_id=" & cat_id.ToString & " ORDER BY sub_name ASC", cn)
Dim l_item As ListItem
Do While Not rsSub.EOF
l_item = New ListItem(rsSub("sub_name").Value, rsSub("sub_id").Value)
List2.Items.Add(l_item)
rsSub.MoveNext()
Loop

List2.SelectedValue = sub_id

rsSub.Close()
rsSub = Nothing

cn.Close()
End If
' End If

End If

End Sub

Protected Sub Select1Change(ByVal sender As System.Object, ByVal e As System.EventArgs)

Dim cn As New ADODB.Connection
cn.Open(YBayTools.Constants.ConnectionString)

Dim rs As New ADODB.Recordset
Dim li As ListItem

rs.Open("Select * from subs where cat_ID =" & List1.SelectedItem.Value.ToString, cn)
'rs.Open("Select * from subs where cat_ID ='" & List1.SelectedItem.Text.ToString & "' & List1.SelectedItem.value.ToString", cn)

Dim strCat As String

If List2.Items.Count > 0 Then
List2.Items.Clear()
End If

If rs.BOF And rs.EOF Then
Response.Write("no records found")
Else

Do While Not rs.EOF
li = New ListItem(rs("sub_name").Value, rs("sub_id").Value)
List2.Items.Add(li)
rs.MoveNext()

Loop

End If

rs.Close()
rs = Nothing
cn.Close()You code is done to work with postback. So write client-site code with javascript on the first listBox and in this code, read the DB and populate you other listBox.
That's the thing I am not sure how to write it in jscript.
I need to get the data for both listboxes from a db.
You can use an XmlHttp object from client-side script, but this will probably only work on IE and Mozilla. Other solutions could use a lot of script and hidden frames (beuark!)

Populating Drop Down List

Hi,

I have a drop down list which is bound to a database field and is populated from so. However I want to add 1 of my own items to the drop down list no matter what is in the database. Is this possible?

ThanksYes. You can use the .Items.Add or .Items.Insert to add items to the dropdownlist. Just be sure to do this after you bind your data, because the bind will remove all items from the list.

hope this helps,
sivilian
Thanks very much, solved my problem.
Well the method is working but how we can set the additional item at the top, for example set "Select one State" on top of my drop down.

Thanks
Hello, try that in the page_load:

ddl.Items.Insert(new ListItem("text",value"),0);

regards
Thanks for you time,

Overload resolution failed because no accessible 'Insert' can be called with these arguments:

the aforsaid error return on line

AltShippingAdd.Items.Insert(new ListItem("Carbon", "C"),0);

Thanks in Advance
Hello,

this working fine, i think there is some problem in syntex.

AltShippingAdd.Items.Insert(0, New ListItem("Select One"))

the above statement is working fine but we can't able to define the value of that item is there a way to define the value of this item.

Thanks.
oh! in advance.
Hello, maybe you are right, I messed up with the syntax. but try to have:

AltShippingAdd.Items.Inert(0,new ListItem("Select One","1"))

regards
Thanks Bilal
It works fine
I am glad I was able to help you out my friend. For any further help, please dont hesitate to contact me here at the forums.

regards