Hi,
I am using Visual Web Developer 2005 Express Edition.
I am trying to SELECT three information fields from a table when the Page_Load take place (so I select the info on the fly). The refering page, sends the spesific record id as "Articleid", that looks typically like this: "http://localhost:1424/BelaBela/accom_Contents.aspx?Articleid=2". I need to extract the "Article=2" so that I can access record 2 (in this example).
How do I define the SelectParameters or QueryStingField on the fly so that I can define the WHERE part of my query (see code below). If I remove the WHERE portion, then it works, but it seem to return the very last record in the database, and if I include it, then I get an error "Must declare the scalar variable @.resortid". How do I programatically set it up so that @.resortid contains the value that is associated with "Articleid"?
My code is below.
Thank you for your advise!
Regards
Jan
/******************************************************************************** RETRIEVE INFORMATION FROM DATABASE*******************************************************************************/// specify the data sourcestring connContStr = ConfigurationManager.ConnectionStrings["tourism_connect1"].ConnectionString;SqlConnection myConn =new SqlConnection(connContStr);// define the command queryString query ="SELECT resortid, TourismGrading, resortHits FROM Resorts WHERE ([resortid] = @.resortid)";SqlCommand myCommand =new SqlCommand(query, myConn);// open the connection and instantiate a datareadermyConn.Open();SqlDataReader myReader = myCommand.ExecuteReader();// loop thru the readerwhile (myReader.Read()){ Label5.Text = myReader.GetInt32(0).ToString(); Label6.Text = myReader.GetInt32(1).ToString(); Label7.Text = myReader.GetInt32(2).ToString();}// close the reader and the connectionmyReader.Close();myConn.Close();
You can try the following, but you may need to change the resotid if it is an integer type.
SqlCommand myCommand =new SqlCommand(query, myConn);
myCommand.Parameters.Add("@.resortid"
,SqlDbType.NVarChar, 10).Value =Request.QueryString("resortid");|||Something like this:
String ArticleID;if (Request.QueryString["ArticleID"] !=null) ArticleID = Request.QueryString["ArticleID"];else// handle bad parameterSqlParameter param =new SqlParameter();param.ParameterName ="@.resortId";param.Value = ArticleID.ToInt32();myConn.Parameters.Add(param);|||
Hi Limno and SGWellens,
Thanks for your help - I managed to get it working like a charm!
Regards
Jan
No comments:
Post a Comment