| | |
create sqldatasource through coding
Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 85
Reputation:
Solved Threads: 3
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
thnks in advance
Johnny.G
•
•
Join Date: Jan 2008
Posts: 2,052
Reputation:
Solved Threads: 118
ASP.NET Syntax (Toggle Plain Text)
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { SqlDataSource mySource = new SqlDataSource("Data Source=SERKAN\\MYSQLSERVER2005;Initial Catalog=deneme;Integrated Security=True;Pooling=False", "select * from names"); myDropDown.DataSource = mySource.Select(DataSourceSelectArguments.Empty); myDropDown.DataValueField = "PKID"; myDropDown.DataTextField = "name"; myDropDown.DataBind(); } }
Due to lack of freedom of speech, i no longer post on this website.
•
•
Join Date: Feb 2008
Posts: 85
Reputation:
Solved Threads: 3
•
•
•
•
ASP.NET Syntax (Toggle Plain Text)
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { SqlDataSource mySource = new SqlDataSource("Data Source=SERKAN\\MYSQLSERVER2005;Initial Catalog=deneme;Integrated Security=True;Pooling=False", "select * from names"); myDropDown.DataSource = mySource.Select(DataSourceSelectArguments.Empty); myDropDown.DataValueField = "PKID"; myDropDown.DataTextField = "name"; myDropDown.DataBind(); } }
Johnny.G
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.
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"
"The best place to find a helping hand is at the end of your own arm"
•
•
Join Date: Feb 2008
Posts: 85
Reputation:
Solved Threads: 3
•
•
•
•
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.
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
•
•
Join Date: Feb 2008
Posts: 85
Reputation:
Solved Threads: 3
•
•
•
•
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
ASP.NET Syntax (Toggle Plain Text)
dbconn.Open() If Not Page.IsPostBack Then dbadap = New SqlDataAdapter("select fname from details", dbconn) dbadap.Fill(ds, "details") DropDownList1.DataSource = ds.Tables("details").DefaultView DropDownList1.DataBind() DropDownList1.SelectedValue = "" MsgBox(DropDownList1.SelectedValue) DropDownList1.DataValueField = "srno" DropDownList1.DataTextField = "fname" 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
•
•
Join Date: Feb 2008
Posts: 85
Reputation:
Solved Threads: 3
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
well this works for me,,thnks for all ur help
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)
Dim conn = New SqlConnection("Data Source=(local);Initial Catalog=(databasename);user id=;pwd=;Trusted_Connection=yes") conn.open() Dim dr As SqlDataReader Dim cmd = New SqlCommand("select * from details", conn) dr = cmd.ExecuteReader() DropDownList1.DataSource = dr DropDownList1.DataTextField = "fname" DropDownList1.DataValueField = "srno" DropDownList1.DataBind()
Johnny.G
![]() |
Other Threads in the ASP.NET Forum
- Previous Thread: Load image in report in runtime in ASP.Net
- Next Thread: ASP.net - Flash Postback problem : Please Help
| Thread Tools | Search this Thread |
.net 2.0 3.5 activexcontrol advice ajax alltypeofvideos asp asp.net bc30451 bottomasp.net browser businesslogiclayer button c# c#gridviewcolumn checkbox child class click commonfunctions compatible confirmationcodegeneration content contenttype countryselector courier css database datagrid datagridview datagridviewcheckbox datalist deadlock development dgv dropdownlist dropdownmenu dynamic edit expose feedback flash flv form formatdecimal forms formview gridview homeedition hosting iframe iis javascript jquery list login menu microsoft mono mssql multistepregistration nameisnotdeclared numerical objects order panelmasterpagebuttoncontrols problem ratings rotatepage save schoolproject search security serializesmo.table silverlight smartcard sql sqlserver2005 suse textbox tracking typeof unauthorized validation vb.net video virtualdirectory vista visual-studio visualstudio vs2008 web webarchitecture webdevelopemnt webdevelopment webservice wizard xml youareanotmemberofthedebuggerusers






