Sunday, March 11, 2012

displaying one data record

Hi all, this is a very basic question of diplaying a data.
on my aspx page I have datasource that will return only ONE record.

<asp:SqlDataSource ID="sdsCategoryName" runat="server" ConnectionString="<%$ ConnectionStrings:KaruselaConnectionString%>"
SelectCommand="SELECT Title FROM tbh_Categories WHERE CategoryID=@.categoryID ">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="-1" Name="CategoryID" QueryStringField="id" Type="int32"/>
</SelectParameters>
</asp:SqlDataSource>

on the server side I would like to manipulate the title of the page according to the data returned from the query:

and on the behind code

if (!this.IsPostBack && !string.IsNullOrEmpty(this.Request.QueryString["ID"])) { DataView dv2 = (DataView)sdsCategoryName.Select(arg);this.Title =string.Format(this.Title, dv2.Table.Columns[0].ToString()); dv2.Dispose(); }
 
 
of course it doesn't work. my question is this. do we really have to put the query datasource on the client side?
and secondly, how can I view the recorsd I recieves from the query?
Thanks for the help.
 

on yours code behind file write this code>>>>

add namespace

System.Data.SqlClient;

and on load event write

SqlConnection sqlconn = new SqlConnection("yourconnectionstring");

SqlCommand sqlcomd=new SqlCommad("SELECT Title FROM tbh_Categories WHERE CategoryID=@.categoryID ",sqlconn);

sqlcomd.CommandType= CommandType.Text;

sqlcomd.Parameter.Add(new SqlParameter("@.catergoryID") ,SqlDbType.NChar ,10);

sqlcomd.Parameter["@.categoryID"].Value = Request.QueryString["ID"];

SqlDataAdapter myadp = new SqlDataAdapter(sqlcomd);

DataTable dt = new DataTable();
myadp.Fill(dt);

Label1.Text= dt.Rows[0].ItemArray.GetValue (0).ToString ();

Mark post as answer if it helped u>>>

Have a great day!

No comments:

Post a Comment