create sqldatasource through coding

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 85
Reputation: johnny.g is an unknown quantity at this point 
Solved Threads: 3
johnny.g johnny.g is offline Offline
Junior Poster in Training

create sqldatasource through coding

 
0
  #1
Jun 25th, 2008
hi all i m using asp.net 2005 and sql server 2005...i have a dropdown list in my page...i have a table called as details...all i want to do is bind the fname from details table to dropdownlistbox...but the thing is tht i want to each and every step through coding...is it possible to do so....pls let me know how to do it,,,,,,,frm whr do i strt ...
thnks in advance
Johnny.G
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: create sqldatasource through coding

 
1
  #2
Jun 25th, 2008
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (!IsPostBack)
  4. {
  5. SqlDataSource mySource = new SqlDataSource("Data Source=SERKAN\\MYSQLSERVER2005;Initial Catalog=deneme;Integrated Security=True;Pooling=False", "select * from names");
  6. myDropDown.DataSource = mySource.Select(DataSourceSelectArguments.Empty);
  7. myDropDown.DataValueField = "PKID";
  8. myDropDown.DataTextField = "name";
  9. myDropDown.DataBind();
  10. }
  11. }
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 85
Reputation: johnny.g is an unknown quantity at this point 
Solved Threads: 3
johnny.g johnny.g is offline Offline
Junior Poster in Training

Re: create sqldatasource through coding

 
0
  #3
Jun 26th, 2008
Originally Posted by serkansendur View Post
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (!IsPostBack)
  4. {
  5. SqlDataSource mySource = new SqlDataSource("Data Source=SERKAN\\MYSQLSERVER2005;Initial Catalog=deneme;Integrated Security=True;Pooling=False", "select * from names");
  6. myDropDown.DataSource = mySource.Select(DataSourceSelectArguments.Empty);
  7. myDropDown.DataValueField = "PKID";
  8. myDropDown.DataTextField = "name";
  9. myDropDown.DataBind();
  10. }
  11. }
coool mann,,,,thts working fine,,,,,well i m using a connection string called as connectionstring2,,,,,,,,,,how can i use this connection string instead of writing the whole connection on pageload ,,,thnks for ur help,,
Johnny.G
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 85
Reputation: johnny.g is an unknown quantity at this point 
Solved Threads: 3
johnny.g johnny.g is offline Offline
Junior Poster in Training

Re: create sqldatasource through coding

 
0
  #4
Jun 26th, 2008
guys how can i use the same code without using sql datasource,,,i came to know tht we can use sql data reader and sql command and other such things,,,,is it possible,,,,let me know,,
thnks in advance
Johnny.G
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 125
Reputation: bala24 is an unknown quantity at this point 
Solved Threads: 11
bala24's Avatar
bala24 bala24 is offline Offline
Junior Poster

Re: create sqldatasource through coding

 
0
  #5
Jun 26th, 2008
Yes you can. Follow these steps.

1).Create a query which fetches the fname from details table.
2).Call cmd.ExecuteReader with this query.
3).Now bind the dropdownlist from the datareade.

For this step you can use the following code.

<dropdownlist>.DataSource = <datareader>
<dropdownlist>.DataBind()
Michelangelo

"The best place to find a helping hand is at the end of your own arm"
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 85
Reputation: johnny.g is an unknown quantity at this point 
Solved Threads: 3
johnny.g johnny.g is offline Offline
Junior Poster in Training

Re: create sqldatasource through coding

 
0
  #6
Jun 26th, 2008
Originally Posted by bala24 View Post
Yes you can. Follow these steps.

1).Create a query which fetches the fname from details table.
2).Call cmd.ExecuteReader with this query.
3).Now bind the dropdownlist from the datareade.

For this step you can use the following code.
hi bala,,,well i m not able to do it,,,,
here is my code

If Not Page.IsPostBack Then
Dim conn As SqlConnection
Dim cmd As SqlCommand
Dim dr As SqlDataReader
Dim str1 As String
Dim strcon As String

str1 = "select * from details"
strcon = "Data Source=PC-1;Initial Catalog=jay;user id=;pwd=;Trusted_Connection=yes"
conn = New SqlConnection(strcon)
conn.Open()
cmd = New SqlCommand(str1, conn)

dr = cmd.ExecuteReader()
DropDownList1.DataSource = dr
DropDownList1.DataValueField = "srno"
DropDownList1.DataTextField = "fname"
DropDownList1.DataBind()
______________________________________
it gives an error...any suggestions,,i have written ths code on page load,,,reply asap
Johnny.G
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 125
Reputation: bala24 is an unknown quantity at this point 
Solved Threads: 11
bala24's Avatar
bala24 bala24 is offline Offline
Junior Poster

Re: create sqldatasource through coding

 
0
  #7
Jun 26th, 2008
Well, your sql query is fetching all columns from the table and your dropdownlist cannot be bound to such a recordset.
What you need to do is modify your sql query to fetch only fname as in

select fname from details
Michelangelo

"The best place to find a helping hand is at the end of your own arm"
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 85
Reputation: johnny.g is an unknown quantity at this point 
Solved Threads: 3
johnny.g johnny.g is offline Offline
Junior Poster in Training

Re: create sqldatasource through coding

 
0
  #8
Jun 26th, 2008
Originally Posted by bala24 View Post
Well, your sql query is fetching all columns from the table and your dropdownlist cannot be bound to such a recordset.
What you need to do is modify your sql query to fetch only fname as in

select fname from details
no its not workin bala,,,,well i have tried another code,,here it is
  1. dbconn.Open()
  2. If Not Page.IsPostBack Then
  3. dbadap = New SqlDataAdapter("select fname from details", dbconn)
  4. dbadap.Fill(ds, "details")
  5. DropDownList1.DataSource = ds.Tables("details").DefaultView
  6. DropDownList1.DataBind()
  7. DropDownList1.SelectedValue = ""
  8. MsgBox(DropDownList1.SelectedValue)
  9. DropDownList1.DataValueField = "srno"
  10. DropDownList1.DataTextField = "fname"
  11. End If

well in this code,,the dropdown list gets filled with system.data.datarowview

i want tht to fill with fname...reply asap
Johnny.G
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 125
Reputation: bala24 is an unknown quantity at this point 
Solved Threads: 11
bala24's Avatar
bala24 bala24 is offline Offline
Junior Poster

Re: create sqldatasource through coding

 
0
  #9
Jun 26th, 2008
Michelangelo

"The best place to find a helping hand is at the end of your own arm"
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 85
Reputation: johnny.g is an unknown quantity at this point 
Solved Threads: 3
johnny.g johnny.g is offline Offline
Junior Poster in Training

Re: create sqldatasource through coding

 
0
  #10
Jun 26th, 2008
Originally Posted by bala24 View Post
Try this.

http://bytes.com/forum/thread259618.html
thnks dude ,,,will go thr tht link n let u know if it wrks for me,,,,
by the way i found a the code tht i wanted finally,,,here it is ,its very simple using only sql data reader,,,here it is
  1. Dim conn = New SqlConnection("Data Source=(local);Initial Catalog=(databasename);user id=;pwd=;Trusted_Connection=yes")
  2. conn.open()
  3. Dim dr As SqlDataReader
  4. Dim cmd = New SqlCommand("select * from details", conn)
  5. dr = cmd.ExecuteReader()
  6. DropDownList1.DataSource = dr
  7. DropDownList1.DataTextField = "fname"
  8. DropDownList1.DataValueField = "srno"
  9. DropDownList1.DataBind()
well this works for me,,thnks for all ur help
Johnny.G
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC