Friday, May 21, 2010

How to select from database with specific where condition in ASP.NET?

Here is code


%26lt;asp:SqlDataSource


id="SqlDataSource1"


runat="server"


ProviderName="System.Data.Odbc"


DataSourceMode="DataSet"


ConnectionString="dsn=yada;uid=root;pwd=...


SelectCommand="SELECT * FROM catalog WHERE Song_Type='Pop'"%26gt;


%26lt;/asp:SqlDataSource%26gt;


Right now all it does is select * from that table where the song type is "Pop" how can I pass a session variable to it so that it selects anything where the value equals to a session variable?

How to select from database with specific where condition in ASP.NET?
try w3schools.com n search for asp.net it will help u alot n here's another code for classic asp but that example for my website u can change it to suit urs of course in the previous page u will refer to a new page n write in that new page this code n don forget to change the table name to be urs n the attributes





%26lt;%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%%26gt;


%26lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-...


%26lt;html xmlns="http://www.w3.org/1999/xhtml"%26gt;


%26lt;head%26gt;


%26lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /%26gt;


%26lt;title%26gt;Untitled Document%26lt;/title%26gt;


%26lt;/head%26gt;





%26lt;body%26gt;





%26lt;%


firstname=request.form("FirstName2")


lastname=request.form("FirstName22")


email=request.form("FirstName23")


pword=request.form("LastName2")


confirmpassword=request.form("LastName...


secretquestion=request.form("FirstName...


secretanswer=request.form("FirstName25...


gender=request.form("select")


occupation=request.form("FirstName26")


'newsletter=request.form("remeber")





set conn=Server.CreateObject("ADODB.Connecti...


conn.Provider="Microsoft.Jet.OLEDB.4.0...


conn.Open "d:\oursitedb.mdb"


set rs=Server.CreateObject("ADODB.recordset"...


rs.open "insert into uUser(FirstName,LastName,Email,Pword,Con... values ('"%26amp;firstname%26amp;"','"%26amp;lastname%26amp;"','"%26amp;email... ",conn





response.redirect("tynote.asp")





%%26gt;


%26lt;% %%26gt;


%26lt;/body%26gt;
Reply:You probably don't want to use session variables, but this should work:





string SQL = "";


string SongType = "";





SongType = Session["SongType"].ToString();





SQL = String.Format("SELECT * FROM Catalog WHERE Song_Type = '{0}' ", SongType);





Then, simply set the SQL string variable as your SelectCommand parameter.


No comments:

Post a Comment