.NET Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgramming.NET Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 27th, 2004, 06:17 PM
DotNetFreak DotNetFreak is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 1 DotNetFreak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
ASP.NET and data binding a dropdown list with SQL Server and C# code

Hey guys I am wondering if u can help me out with something.
I can't seem to fill the dropdown list on my .aspx file =S
=============================================
Let just say i have createnewaccount.aspx in it is a aspx form and this dropdown:
=============================================
<aspropDownList id="dpdProvinces" runat="server"></aspropDownList>


==============================================
Then in my createnewaccount.cs file I have the following function
==============================================
//Fill a dropdown list Parameters: Dropdown id, database table name
public static bool FillDropDownList(DropDownList dDl,string tblName)
{
const string strServer = "TestServer";
const string strDBId = "SA";
const string strDBPwd = "";
const string strDBName = "dotNetDemo";
//Connection string for SQL Server DB
string ConnectionString =
"server=" + strServer +
";uid=" + strDBId +
";pwd=" + strDBPwd +
";database=" + strDBName + ";";
//Set the Data Value and Text fields to the db column
dDl.DataValueField = "Id";
dDl.DataTextField = "Name";

//SQL string to execute
string strSQL = "SELECT * FROM " + tblName;
//Create a connection
SqlConnection conn = new SqlConnection(ConnectionString);
try
{
conn.Open();
//Execute the SQL String
SqlCommand cmd = new SqlCommand(strSQL, conn);
SqlDataReader reader = cmd.ExecuteReader();
dDl.DataSource = reader;
dDl.DataBind();
conn.Close();
//Insert a item at the begining of the list
dDl.Items.Insert(0, "<--- Select --->");

}
catch // (Exception e) // Exception Removed
{
return false;

}
finally
{
conn.Close();
}
return true;
}


private void Page_Load(object sender, System.EventArgs e)
{
FillDropDownList(dpdProvinces, "Province");
}


Now should that work already by the databind i did with the function or do i have to loop through the table data and fill in the function? i tried both ways and doesn't seem to work. I kno my connection string works because I tried it somewhere else.. and I have data in the Province table and the fields are named correctly.. any suggestions would be appreciated.. thanks!!!

Reply With Quote
  #2  
Old January 27th, 2004, 11:15 PM
nageshtechtips nageshtechtips is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 8 nageshtechtips User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up ASP.NET and data binding a dropdown list with SQL Server and C# code

Hi,

i hope it will helps you. please place this code after reader is excuted.


while (reader.Read())
{
dDl.Items.Add(new ListItem(reader.GetString(1),reader.GetString(0))) ;
}

//dDl.DataSource = reader;
//dDl.DataBind();


Regards,
Nagesh

Reply With Quote
  #3  
Old January 29th, 2004, 11:26 PM
CountFLY CountFLY is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Toronto, Canada
Posts: 23 CountFLY User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0

Reply With Quote
  #4  
Old January 31st, 2004, 06:54 AM
venu pullela venu pullela is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 1 venu pullela User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Lightbulb Hyderabad

For ur problem u need to add one more line in ur code
dDl.DataTextField="xyz" after data sorce.


dDl.DataSource = reader;
here................
dDl.DataBind();

in place of "xyz" u need to specify the field name u want to fill the dropdown list with.

Reply With Quote
  #5  
Old August 10th, 2008, 01:44 PM
jayantpatil812 jayantpatil812 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2008
Posts: 1 jayantpatil812 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 31 sec
Reputation Power: 0
Thumbs up

Try this sample code

#region BindCountry
private void BindCountry()
{
SqlConnection Con = new SqlConnection();
string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlCommand SqlCmd = new SqlCommand();
string Str="select * from dbo.Country";
SqlCmd.CommandText = Str;
Con.ConnectionString = ConnectionString;



DataSet DS = new DataSet();
SqlDataAdapter SqlAdp = new SqlDataAdapter(Str,Con);
SqlAdp.Fill(DS,"Country");

//ddlCountry.DataSource = DS;

if (DS.Tables[0].Rows.Count > 0)
{
foreach (DataRow Dr in DS.Tables[0].Rows)
{
ddlCountry.Items.Add(new ListItem(Dr["countryName"].ToString(), Dr["countryID"].ToString()));
}
}
}
#endregion

and call function in pageload

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgramming.NET Development > ASP.NET and data binding a dropdown list with SQL Server and C# code


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap