943,864 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Marked Solved
  • Views: 6317
  • ASP.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 25th, 2008
0

create sqldatasource through coding

Expand Post »
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
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
johnny.g is offline Offline
91 posts
since Feb 2008
Jun 25th, 2008
1

Re: create sqldatasource through coding

ASP.NET Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Jun 26th, 2008
0

Re: create sqldatasource through coding

ASP.NET Syntax (Toggle Plain Text)
  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,,
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
johnny.g is offline Offline
91 posts
since Feb 2008
Jun 26th, 2008
0

Re: create sqldatasource through coding

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
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
johnny.g is offline Offline
91 posts
since Feb 2008
Jun 26th, 2008
0

Re: create sqldatasource through coding

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.

Quote ...
<dropdownlist>.DataSource = <datareader>
<dropdownlist>.DataBind()
Reputation Points: 15
Solved Threads: 11
Junior Poster
bala24 is offline Offline
125 posts
since Oct 2006
Jun 26th, 2008
0

Re: create sqldatasource through coding

Click to Expand / Collapse  Quote originally posted by bala24 ...
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
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
johnny.g is offline Offline
91 posts
since Feb 2008
Jun 26th, 2008
0

Re: create sqldatasource through coding

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
Reputation Points: 15
Solved Threads: 11
Junior Poster
bala24 is offline Offline
125 posts
since Oct 2006
Jun 26th, 2008
0

Re: create sqldatasource through coding

Click to Expand / Collapse  Quote originally posted by bala24 ...
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
ASP.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
johnny.g is offline Offline
91 posts
since Feb 2008
Jun 26th, 2008
0

Re: create sqldatasource through coding

Reputation Points: 15
Solved Threads: 11
Junior Poster
bala24 is offline Offline
125 posts
since Oct 2006
Jun 26th, 2008
0

Re: create sqldatasource through coding

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
ASP.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
johnny.g is offline Offline
91 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Load image in report in runtime in ASP.Net
Next Thread in ASP.NET Forum Timeline: ASP.net - Flash Postback problem : Please Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC