|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Jul 2009
Posts: 608
|
Drop down List
Hey,
I have the following list box:- Code:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="cat_name"
DataValueField="cat_name" Width="258px">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Data Source=tcp:esql2k801.discountasp.net;Initial Catalog=SQL2008_673062_kids;User ID=SQL2008_673062_kids_user;Password=*******;"
SelectCommand="SELECT [cat_id], [cat_name] FROM [tbl_gallery_cat]">
</asp:SqlDataSource>
I have a button, and on button click i have the following code:- PHP Code:
Take a look at this page to see it in action:- http://kidsunlimi2.eweb102.discounta...galleries.aspx How can i write the code to fix his problem? Regards |
|
|
|
|
|
#2 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: Cape Town, South Africa
Posts: 1,587
|
Ok, first things first.
You would want to move that connection string to the web.config instead so that you do not declare it over and over again. Imagine if you connection string changes when its like this. I am not a fan of using a sqlDataSource to bind like this, as its hard to tell what exactly is going on and when its getting bound if you dont no how it works. Next, the command. Do not use parameters.Add. Use this: dbCommandImage.Parameters.AddWithValue("@cat_name", DropDownList1.SelectedValue); Next, you would want to use a repeater instead of of just writing html to a label. And bind the repeater to the datareader. To figure out y the qry is not working, write out the selected value to a label or something to see what its getting. Hope this helps |
|
|
|
|
|
#3 |
|
SitePoint Enthusiast
![]() Join Date: Oct 2009
Location: Mid-West USA
Posts: 30
|
I second NightStalker. Start here:
http://articles.sitepoint.com/articl...peater-control Repeater is great if you want more control over the outputted HTML (without resorting to MVC). Very good article to get you started. Add the connectionstring to the web.config like so: Code:
<connectionStrings> <add name="MyConnectionName" connectionString="Data Source=computername\SQLEXPRESS;Initial Catalog=Databasename;User ID=sa;Password=dev123" providerName="System.Data.SqlClient" /> </connectionStrings> Then, access it like so: Code:
string myConnStr = ConfigurationManager.ConnectionStrings["MyConnectionName"].ConnectionString; |
|
|
|
|
|
#4 | |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Jul 2009
Posts: 608
|
Hey,
Thanks, i actually found what i was doing wrong, i changed the code to this:- PHP Code:
Quote:
Regards |
|
|
|
|
|
|
#5 |
|
SitePoint Enthusiast
![]() Join Date: Oct 2009
Location: Mid-West USA
Posts: 30
|
I think it was a string formatting problem. Try this....
string sqlStrImage = "SELECT gallery_id, cat_name, image, caption FROM tbl_gallery_images WHERE cat_name = " + "'" + DropDownList1.SelectedValue + "'"; |
|
|
|
|
|
#6 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: Cape Town, South Africa
Posts: 1,587
|
Yes, USPatriots solution will work, but rather you parameters as it is a lot safer. Doing things like this leaves holes for SQL injection. And i strongly suggest u use a repeater
|
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 05:31.









Linear Mode
