Ask a Question related to ASP.NET General, Design and Development.
-
Reddy #1
Dropdownlist prepopulation
I am trying to prepopulate two drop down meus with results of two differnt
SQL queries. I have written the following code. But I am getting errors. Can
anybody please help me how to do this. I also doubt whether this is the most
efficient way to have maximum performance when lots of users access the web
site.
string dsn = ConfigurationSettings.AppSettings["testDsn"];
SqlConnection connection = new SqlConnection (dsn);
SqlDataAdapter adapter = new SqlDataAdapter ("select * from authors; select
* from publishers", connection);
DataSet ds = new DataSet ();
adapter.Fill (ds, "authors");
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "au_fname";
DropDownList1.DataValueField = "au_id";
DropDownList1.DataBind ();
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf
(DropDownList1.Items.FindByValue ("672-71-3249"));
DataSet ds2 = new DataSet ();
adapter.Fill (ds2, "publishers");
DropDownList2.DataSource = ds2;
DropDownList2.DataTextField = "pub_name";
DropDownList2.DataValueField = "pub_id";
DropDownList2.DataBind ();
DropDownList2.SelectedIndex = DropDownList2.Items.IndexOf
(DropDownList2.Items.FindByValue ("1389"));
Thanks,
Reddy
Reddy Guest
-
DropDownList
Hi I looked everywhere to solve my problem but with no success. On page there is a dropdownlist control which is binded to a database. When I... -
Dropdownlist help please
Hi there, I'm a newbie to Dreamweaver MX 2004 and ASP.NET. I'm having trouble with a simple application that involves the user selecting a value... -
Add a dropDownList
Hello, Could you please give me an exemple how to add a dropdownlist in a datagrid. With this following code, I bind a datatable to a datagrid,... -
HELP with ASP.NET DropDownList Box ! : )
Hi all, I have a DropDownList on my page created in Design Time calledddlDate. In code, I do the following: 1 ddlDate = New DropDownList() <-... -
<asp:DropDownList >
It just doesn't work like that - - you need to either bind data to the datalist in code, or manually add the items, one by one, where you're doing... -
S. Justin Gengo #2
Re: Dropdownlist prepopulation
Reddy,
Your query is returning both recordsets at the same time, but in your code I
don't see where you are moving to the second recordset before binding it to
the second drop down.
Sincerely,
--
S. Justin Gengo, MCP
Web Developer
Free code library at:
[url]www.aboutfortunate.com[/url]
"Out of chaos comes order."
Nietzche
"Reddy" <gksreddy@adsgroup.com> wrote in message
news:uRwxisNXDHA.1004@TK2MSFTNGP12.phx.gbl...Can> I am trying to prepopulate two drop down meus with results of two differnt
> SQL queries. I have written the following code. But I am getting errors.most> anybody please help me how to do this. I also doubt whether this is theweb> efficient way to have maximum performance when lots of users access theselect> site.
>
> string dsn = ConfigurationSettings.AppSettings["testDsn"];
> SqlConnection connection = new SqlConnection (dsn);
> SqlDataAdapter adapter = new SqlDataAdapter ("select * from authors;> * from publishers", connection);
> DataSet ds = new DataSet ();
> adapter.Fill (ds, "authors");
>
> DropDownList1.DataSource = ds;
> DropDownList1.DataTextField = "au_fname";
> DropDownList1.DataValueField = "au_id";
> DropDownList1.DataBind ();
> DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf
> (DropDownList1.Items.FindByValue ("672-71-3249"));
>
> DataSet ds2 = new DataSet ();
> adapter.Fill (ds2, "publishers");
> DropDownList2.DataSource = ds2;
> DropDownList2.DataTextField = "pub_name";
> DropDownList2.DataValueField = "pub_id";
> DropDownList2.DataBind ();
> DropDownList2.SelectedIndex = DropDownList2.Items.IndexOf
> (DropDownList2.Items.FindByValue ("1389"));
>
>
>
> Thanks,
>
> Reddy
>
>
S. Justin Gengo Guest
-
Reddy #3
Re: Dropdownlist prepopulation
Sorry I couldn't put it much better.
I needed to execute two SQL queries and my idea was to get two tables from
two sql queries.
I don't know whether I can execute SQL queries like this or need to execute
them separately
Reddy
"S. Justin Gengo" <sjgengo@aboutfortunate.com> wrote in message
news:%23tX1EiOXDHA.2484@TK2MSFTNGP09.phx.gbl...I> Reddy,
>
> Your query is returning both recordsets at the same time, but in your codeto> don't see where you are moving to the second recordset before binding itdiffernt> the second drop down.
>
> Sincerely,
>
> --
> S. Justin Gengo, MCP
> Web Developer
>
> Free code library at:
> [url]www.aboutfortunate.com[/url]
>
> "Out of chaos comes order."
> Nietzche
>
>
> "Reddy" <gksreddy@adsgroup.com> wrote in message
> news:uRwxisNXDHA.1004@TK2MSFTNGP12.phx.gbl...> > I am trying to prepopulate two drop down meus with results of two> Can> > SQL queries. I have written the following code. But I am getting errors.> most> > anybody please help me how to do this. I also doubt whether this is the> web> > efficient way to have maximum performance when lots of users access the> select> > site.
> >
> > string dsn = ConfigurationSettings.AppSettings["testDsn"];
> > SqlConnection connection = new SqlConnection (dsn);
> > SqlDataAdapter adapter = new SqlDataAdapter ("select * from authors;>> > * from publishers", connection);
> > DataSet ds = new DataSet ();
> > adapter.Fill (ds, "authors");
> >
> > DropDownList1.DataSource = ds;
> > DropDownList1.DataTextField = "au_fname";
> > DropDownList1.DataValueField = "au_id";
> > DropDownList1.DataBind ();
> > DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf
> > (DropDownList1.Items.FindByValue ("672-71-3249"));
> >
> > DataSet ds2 = new DataSet ();
> > adapter.Fill (ds2, "publishers");
> > DropDownList2.DataSource = ds2;
> > DropDownList2.DataTextField = "pub_name";
> > DropDownList2.DataValueField = "pub_id";
> > DropDownList2.DataBind ();
> > DropDownList2.SelectedIndex = DropDownList2.Items.IndexOf
> > (DropDownList2.Items.FindByValue ("1389"));
> >
> >
> >
> > Thanks,
> >
> > Reddy
> >
> >
>
Reddy Guest
-
Bülent Keskin #4
Re: Dropdownlist prepopulation
You should write as following:
----------------------------------
public DataTable GetDataTable(string sql){
string dsn = ConfigurationSettings.AppSettings["testDsn"];
SqlConnection connection = new SqlConnection (dsn);
SqlDataAdapter adapter = new SqlDataAdapter (sql, connection);
DataSet ds = new DataSet ();
adapter.Fill (ds);
return ds.Tables[0];
}
public void Page_Load()
{
if(!IsPostBack)
{
DropDownList1.DataSource = GetDataTable("select * from authors");
DropDownList1.DataTextField = "au_fname";
DropDownList1.DataValueField = "au_id";
DropDownList1.DataBind ();
DropDownList1.Items.FindByValue ("672-71-3249").Selected = true;
DropDownList2.DataSource = GetDataTable("select * from publishers");
DropDownList2.DataTextField = "pub_name";
DropDownList2.DataValueField = "pub_id";
DropDownList2.DataBind ();
DropDownList2.Items.FindByValue ("1389").Selected = true;
}
}
"Reddy" <gksreddy@adsgroup.com> wrote in message
news:uRwxisNXDHA.1004@TK2MSFTNGP12.phx.gbl...Can> I am trying to prepopulate two drop down meus with results of two differnt
> SQL queries. I have written the following code. But I am getting errors.most> anybody please help me how to do this. I also doubt whether this is theweb> efficient way to have maximum performance when lots of users access theselect> site.
>
> string dsn = ConfigurationSettings.AppSettings["testDsn"];
> SqlConnection connection = new SqlConnection (dsn);
> SqlDataAdapter adapter = new SqlDataAdapter ("select * from authors;> * from publishers", connection);
> DataSet ds = new DataSet ();
> adapter.Fill (ds, "authors");
>
> DropDownList1.DataSource = ds;
> DropDownList1.DataTextField = "au_fname";
> DropDownList1.DataValueField = "au_id";
> DropDownList1.DataBind ();
> DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf
> (DropDownList1.Items.FindByValue ("672-71-3249"));
>
> DataSet ds2 = new DataSet ();
> adapter.Fill (ds2, "publishers");
> DropDownList2.DataSource = ds2;
> DropDownList2.DataTextField = "pub_name";
> DropDownList2.DataValueField = "pub_id";
> DropDownList2.DataBind ();
> DropDownList2.SelectedIndex = DropDownList2.Items.IndexOf
> (DropDownList2.Items.FindByValue ("1389"));
>
>
>
> Thanks,
>
> Reddy
>
>
Bülent Keskin Guest
-
S. Justin Gengo #5
Re: Dropdownlist prepopulation
Reddy,
You can certainly do it this way.
Just specify the datamember (the table name) for each drop down list as Jeff
says.
Sincerely,
--
S. Justin Gengo, MCP
Web Developer
Free code library at:
[url]www.aboutfortunate.com[/url]
"Out of chaos comes order."
Nietzche
"Reddy" <gksreddy@adsgroup.com> wrote in message
news:OkX32wOXDHA.2632@TK2MSFTNGP09.phx.gbl...execute> Sorry I couldn't put it much better.
>
> I needed to execute two SQL queries and my idea was to get two tables from
> two sql queries.
>
> I don't know whether I can execute SQL queries like this or need tocode> them separately
>
> Reddy
>
>
> "S. Justin Gengo" <sjgengo@aboutfortunate.com> wrote in message
> news:%23tX1EiOXDHA.2484@TK2MSFTNGP09.phx.gbl...> > Reddy,
> >
> > Your query is returning both recordsets at the same time, but in yourerrors.> I> to> > don't see where you are moving to the second recordset before binding it> differnt> > the second drop down.
> >
> > Sincerely,
> >
> > --
> > S. Justin Gengo, MCP
> > Web Developer
> >
> > Free code library at:
> > [url]www.aboutfortunate.com[/url]
> >
> > "Out of chaos comes order."
> > Nietzche
> >
> >
> > "Reddy" <gksreddy@adsgroup.com> wrote in message
> > news:uRwxisNXDHA.1004@TK2MSFTNGP12.phx.gbl...> > > I am trying to prepopulate two drop down meus with results of two> > > SQL queries. I have written the following code. But I am gettingthe> > Can> > > anybody please help me how to do this. I also doubt whether this isthe> > most> > > efficient way to have maximum performance when lots of users access>> > web> > select> > > site.
> > >
> > > string dsn = ConfigurationSettings.AppSettings["testDsn"];
> > > SqlConnection connection = new SqlConnection (dsn);
> > > SqlDataAdapter adapter = new SqlDataAdapter ("select * from authors;> >> > > * from publishers", connection);
> > > DataSet ds = new DataSet ();
> > > adapter.Fill (ds, "authors");
> > >
> > > DropDownList1.DataSource = ds;
> > > DropDownList1.DataTextField = "au_fname";
> > > DropDownList1.DataValueField = "au_id";
> > > DropDownList1.DataBind ();
> > > DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf
> > > (DropDownList1.Items.FindByValue ("672-71-3249"));
> > >
> > > DataSet ds2 = new DataSet ();
> > > adapter.Fill (ds2, "publishers");
> > > DropDownList2.DataSource = ds2;
> > > DropDownList2.DataTextField = "pub_name";
> > > DropDownList2.DataValueField = "pub_id";
> > > DropDownList2.DataBind ();
> > > DropDownList2.SelectedIndex = DropDownList2.Items.IndexOf
> > > (DropDownList2.Items.FindByValue ("1389"));
> > >
> > >
> > >
> > > Thanks,
> > >
> > > Reddy
> > >
> > >
> >
>
S. Justin Gengo Guest
-
S. Justin Gengo #6
Re: Dropdownlist prepopulation
Reddy,
I was just experimenting with some code similar to yours and I noticed that
the tables aren't named. Here's what you need to do:
1) The way you are creating your connection and filling your first dataset
is just fine. The dataset is actually being populated with two tables each
based on one of the two select statements you are using in the SqlCommand.
2) You won't need the second dataset at all.
3) The tables do not have string names. You'll need to refer to them by
index number.
4) When you assign a datatable to the datalist, quick way to specify the
correct datasource would be to specify the correct datatable directly:
DropDownList1.DataSource = ds.Tables(0);
DropDownList2.DataSource = ds.Tables(1);
Sincerely,
--
S. Justin Gengo, MCP
Web Developer
Free code library at:
[url]www.aboutfortunate.com[/url]
"Out of chaos comes order."
Nietzche
"S. Justin Gengo" <sjgengo@aboutfortunate.com> wrote in message
news:%23tX1EiOXDHA.2484@TK2MSFTNGP09.phx.gbl...I> Reddy,
>
> Your query is returning both recordsets at the same time, but in your codeto> don't see where you are moving to the second recordset before binding itdiffernt> the second drop down.
>
> Sincerely,
>
> --
> S. Justin Gengo, MCP
> Web Developer
>
> Free code library at:
> [url]www.aboutfortunate.com[/url]
>
> "Out of chaos comes order."
> Nietzche
>
>
> "Reddy" <gksreddy@adsgroup.com> wrote in message
> news:uRwxisNXDHA.1004@TK2MSFTNGP12.phx.gbl...> > I am trying to prepopulate two drop down meus with results of two> Can> > SQL queries. I have written the following code. But I am getting errors.> most> > anybody please help me how to do this. I also doubt whether this is the> web> > efficient way to have maximum performance when lots of users access the> select> > site.
> >
> > string dsn = ConfigurationSettings.AppSettings["testDsn"];
> > SqlConnection connection = new SqlConnection (dsn);
> > SqlDataAdapter adapter = new SqlDataAdapter ("select * from authors;>> > * from publishers", connection);
> > DataSet ds = new DataSet ();
> > adapter.Fill (ds, "authors");
> >
> > DropDownList1.DataSource = ds;
> > DropDownList1.DataTextField = "au_fname";
> > DropDownList1.DataValueField = "au_id";
> > DropDownList1.DataBind ();
> > DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf
> > (DropDownList1.Items.FindByValue ("672-71-3249"));
> >
> > DataSet ds2 = new DataSet ();
> > adapter.Fill (ds2, "publishers");
> > DropDownList2.DataSource = ds2;
> > DropDownList2.DataTextField = "pub_name";
> > DropDownList2.DataValueField = "pub_id";
> > DropDownList2.DataBind ();
> > DropDownList2.SelectedIndex = DropDownList2.Items.IndexOf
> > (DropDownList2.Items.FindByValue ("1389"));
> >
> >
> >
> > Thanks,
> >
> > Reddy
> >
> >
>
S. Justin Gengo Guest
-
S. Justin Gengo #7
Re: Dropdownlist prepopulation
Reddy,
Ok, I've switched from coffee to mountain dew and my brain is functioning
better. I realized I gave you bad code here.
You need to eliminate "authors" from your fill command if you populate the
datasets the way I suggested.
Change it from this:
adapter.Fill (ds, "authors");
to this:
adapter.Fill(ds);
Sorry about that!
--
S. Justin Gengo, MCP
Web Developer
Free code library at:
[url]www.aboutfortunate.com[/url]
"Out of chaos comes order."
Nietzche
"S. Justin Gengo" <sjgengo@aboutfortunate.com> wrote in message
news:uBA84tPXDHA.2648@TK2MSFTNGP09.phx.gbl...that> Reddy,
>
> I was just experimenting with some code similar to yours and I noticedcode> the tables aren't named. Here's what you need to do:
>
> 1) The way you are creating your connection and filling your first dataset
> is just fine. The dataset is actually being populated with two tables each
> based on one of the two select statements you are using in the SqlCommand.
>
> 2) You won't need the second dataset at all.
>
> 3) The tables do not have string names. You'll need to refer to them by
> index number.
>
> 4) When you assign a datatable to the datalist, quick way to specify the
> correct datasource would be to specify the correct datatable directly:
>
> DropDownList1.DataSource = ds.Tables(0);
>
> DropDownList2.DataSource = ds.Tables(1);
>
> Sincerely,
>
> --
> S. Justin Gengo, MCP
> Web Developer
>
> Free code library at:
> [url]www.aboutfortunate.com[/url]
>
> "Out of chaos comes order."
> Nietzche
>
>
> "S. Justin Gengo" <sjgengo@aboutfortunate.com> wrote in message
> news:%23tX1EiOXDHA.2484@TK2MSFTNGP09.phx.gbl...> > Reddy,
> >
> > Your query is returning both recordsets at the same time, but in yourerrors.> I> to> > don't see where you are moving to the second recordset before binding it> differnt> > the second drop down.
> >
> > Sincerely,
> >
> > --
> > S. Justin Gengo, MCP
> > Web Developer
> >
> > Free code library at:
> > [url]www.aboutfortunate.com[/url]
> >
> > "Out of chaos comes order."
> > Nietzche
> >
> >
> > "Reddy" <gksreddy@adsgroup.com> wrote in message
> > news:uRwxisNXDHA.1004@TK2MSFTNGP12.phx.gbl...> > > I am trying to prepopulate two drop down meus with results of two> > > SQL queries. I have written the following code. But I am gettingthe> > Can> > > anybody please help me how to do this. I also doubt whether this isthe> > most> > > efficient way to have maximum performance when lots of users access>> > web> > select> > > site.
> > >
> > > string dsn = ConfigurationSettings.AppSettings["testDsn"];
> > > SqlConnection connection = new SqlConnection (dsn);
> > > SqlDataAdapter adapter = new SqlDataAdapter ("select * from authors;> >> > > * from publishers", connection);
> > > DataSet ds = new DataSet ();
> > > adapter.Fill (ds, "authors");
> > >
> > > DropDownList1.DataSource = ds;
> > > DropDownList1.DataTextField = "au_fname";
> > > DropDownList1.DataValueField = "au_id";
> > > DropDownList1.DataBind ();
> > > DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf
> > > (DropDownList1.Items.FindByValue ("672-71-3249"));
> > >
> > > DataSet ds2 = new DataSet ();
> > > adapter.Fill (ds2, "publishers");
> > > DropDownList2.DataSource = ds2;
> > > DropDownList2.DataTextField = "pub_name";
> > > DropDownList2.DataValueField = "pub_id";
> > > DropDownList2.DataBind ();
> > > DropDownList2.SelectedIndex = DropDownList2.Items.IndexOf
> > > (DropDownList2.Items.FindByValue ("1389"));
> > >
> > >
> > >
> > > Thanks,
> > >
> > > Reddy
> > >
> > >
> >
>
S. Justin Gengo Guest
-
S. Justin Gengo #8
Re: Dropdownlist prepopulation
Reddy,
The only real way to tell is to try each.
Put them both in a loop and run it say, 10,000 times. Get the start time
before the loop and the end time after and see which is faster.
Please post the results here. I'm sure we're all curious which will be
faster.
Sincerely,
--
S. Justin Gengo, MCP
Web Developer
Free code library at:
[url]www.aboutfortunate.com[/url]
"Out of chaos comes order."
Nietzche
"Reddy" <gksreddy@adsgroup.com> wrote in message
news:uYjB6XZXDHA.2516@TK2MSFTNGP09.phx.gbl...way> Thanks Everyone.
>
> Both the processes work fantastically
>
> I am in a dilemma now. Which of the two processes is the most efficientI> performance wise(when more number of users access the same page). Becuaseselect> want to use some more controls on the same page.
>
>
> private void Page_Load(object sender, System.EventArgs e)
> {
> DropDownList1.DataSource = GetDataTable("select * from authors");
> DropDownList1.DataTextField = "au_fname";
> DropDownList1.DataValueField = "au_id";
> DropDownList1.DataBind ();
> DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf
> (DropDownList1.Items.FindByValue ("672-71-3249"));
>
> DropDownList2.DataSource = GetDataTable("select * from publishers");
> DropDownList2.DataTextField = "pub_name";
> DropDownList2.DataValueField = "pub_id";
> DropDownList2.DataBind ();
> DropDownList2.SelectedIndex = DropDownList2.Items.IndexOf
> (DropDownList2.Items.FindByValue ("1389"));
> }
> public DataTable GetDataTable(string sql)
> {
> string dsn = ConfigurationSettings.AppSettings["testDsn"];
> SqlConnection connection = new SqlConnection (dsn);
> SqlDataAdapter adapter = new SqlDataAdapter (sql, connection);
> DataSet ds = new DataSet ();
> adapter.Fill (ds);
> return ds.Tables[0];
> }
>
>
>
>
>
> or
>
>
>
>
> private void Page_Load(object sender, System.EventArgs e)
> {
> string dsn = ConfigurationSettings.AppSettings["testDsn"];
> SqlConnection connection = new SqlConnection (dsn);
> SqlDataAdapter adapter = new SqlDataAdapter ("select * from authors;differnt> * from publishers", connection);
> DataSet ds = new DataSet ();
> adapter.Fill (ds);
>
> DropDownList1.DataSource = ds.Tables[0];
> DropDownList1.DataTextField = "au_fname";
> DropDownList1.DataValueField = "au_id";
> DropDownList1.DataBind ();
> DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf
> (DropDownList1.Items.FindByValue ("672-71-3249"));
>
> DropDownList2.DataSource = ds.Tables[1];
> DropDownList2.DataTextField = "pub_name";
> DropDownList2.DataValueField = "pub_id";
> DropDownList2.DataBind ();
> DropDownList2.SelectedIndex = DropDownList2.Items.IndexOf
> (DropDownList2.Items.FindByValue ("1389"));
> }
>
>
> Reddy
>
>
>
>
>
> "Reddy" <gksreddy@adsgroup.com> wrote in message
> news:uRwxisNXDHA.1004@TK2MSFTNGP12.phx.gbl...> > I am trying to prepopulate two drop down meus with results of two> Can> > SQL queries. I have written the following code. But I am getting errors.> most> > anybody please help me how to do this. I also doubt whether this is the> web> > efficient way to have maximum performance when lots of users access the> select> > site.
> >
> > string dsn = ConfigurationSettings.AppSettings["testDsn"];
> > SqlConnection connection = new SqlConnection (dsn);
> > SqlDataAdapter adapter = new SqlDataAdapter ("select * from authors;>> > * from publishers", connection);
> > DataSet ds = new DataSet ();
> > adapter.Fill (ds, "authors");
> >
> > DropDownList1.DataSource = ds;
> > DropDownList1.DataTextField = "au_fname";
> > DropDownList1.DataValueField = "au_id";
> > DropDownList1.DataBind ();
> > DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf
> > (DropDownList1.Items.FindByValue ("672-71-3249"));
> >
> > DataSet ds2 = new DataSet ();
> > adapter.Fill (ds2, "publishers");
> > DropDownList2.DataSource = ds2;
> > DropDownList2.DataTextField = "pub_name";
> > DropDownList2.DataValueField = "pub_id";
> > DropDownList2.DataBind ();
> > DropDownList2.SelectedIndex = DropDownList2.Items.IndexOf
> > (DropDownList2.Items.FindByValue ("1389"));
> >
> >
> >
> > Thanks,
> >
> > Reddy
> >
> >
>
S. Justin Gengo Guest



Reply With Quote

