Populate DropDownList


V

Viktor Popov

Hi ,
I'm trying to populate DropDownList from data base but it doesn't work.
Could someone help me!
My code is:

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 () ;

}
 
Ad

Advertisements

Z

Zürcher See

SqlConnection conn = new SqlConnection( ... )
SqlDataAdapter da = new SqlDataAdapter ("SELECT OfferID,TypeOffer FROM
blek.TypeOffer ORDER BY TypeOffer",conn);

DataTable table=new DataTable();
da.Fill(table)

DropDownList2.DataSource=table;
DropDownList2.DataValueField = "OfferID";
DropDownList2.DataTextField = "TypeOffer";
DropDownList2.DataBind ();


Viktor Popov said:
Hi ,
I'm trying to populate DropDownList from data base but it doesn't work.
Could someone help me!
My code is:

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();

If you use a data adapter you don't have to open the connection, the data
adapter does it.
(If you open a connection close it when you are done)
da.SelectCommand.ExecuteReader();

I never used this command with data adapter ... try to leave it away
 
V

Viktor Popov

Thanks for the reply, but It doesn't work and I don't know why. There is no
error...
I'm stucked!
 
Z

Zürcher See

Check if the onload method is called ... check if the table is filled with
rows (Page.Controls.Add(new System.Web.UI.LiteralControl("table
rows="+table.Rows.Count));)
If you are using program like visual studio remove the dropdown control and
place on the page a new one
 
V

Viktor Popov

When I use: msg.Text = table.Rows.Count.ToString();

There is no value...
I'm sending the code again......I use ASP.NET/C#/MS SQL.

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();

}
 
Z

Zürcher See

Do you see the dropdown on the page?

Try to initialize the table manually and add some rows without using the
data adapter

DataTable table=new DataTable();

table.Columns.Add(new DataColumn("OfferID",typeof(int)));
table.Columns.Add(new DataColumn("TypeOffer",typeof(string)));

table.Rows.Add(new object[]{0,"First Row"});
table.Rows.Add(new object[]{1,"Second Row"});

DropDownList2.DataSource=table;
DropDownList2.DataValueField = "OfferID";
DropDownList2.DataTextField = "TypeOffer";
DropDownList2.DataBind ();
 
Ad

Advertisements

V

Viktor Popov

This is the last code... Again it doesn't work. There is nothing in the
dropdownlist and the msg.Text = ""

I don't know what to do. I do the whole day the same thing and I can't make
it....





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();

table.Columns.Add(new DataColumn("OfferID",typeof(int)));

table.Columns.Add(new DataColumn("TypeOffer",typeof(string)));

table.Rows.Add(new object[]{0,"First Row"});

table.Rows.Add(new object[]{1,"Second Row"});

DropDownList2.DataSource=table;

DropDownList2.DataValueField = "OfferID";

DropDownList2.DataTextField = "TypeOffer";

DropDownList2.DataBind();

msg.Text = table.Rows.Count.ToString();

conn.Close();}
 
V

Viktor Popov

Hi, When I placed a button and I wrote the code in OnClick method it works.
How could it work on Page_Load method?
 
Z

Zürcher See

The Page_Load is a method called from the onload event.

If you have a code generated from a program, like visual studio, it should
looks like this:

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)

{

//

// CODEGEN: This call is required by the ASP.NET Web Form Designer.

//

InitializeComponent();

base.OnInit(e);

}


/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion
 
Ad

Advertisements

V

Viktor Popov

Thank you very much. It works :))
I'm so happy:)
Thank you very much! I can go to bed now:)))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top