Thursday, March 22, 2012

A very simple SQL question...

I don't want to use any of the big fancy Data presentation controls like GridView, Datalist, etc. I just want to do a programmatic reading of a SQL DB field and then post its contents to a label's text property. I have programmatic connection and select statements that seem to work okay. The code I have written so far is shown below. I just need to know what code should go in place of the long underlined portion...(oh, and pardon my newbie ignorance)...

<%@. Import Namespace="System.Data" %>
<%@. Import Namespace="System.Web" %>
<%@. Import Namespace="System.Web.Configuration" %>
<%@. Import Namespace="System.Web.UI" %>
<%@. Import Namespace="System.Web.UI.WebControls" %>

<script runat="server">

Sub Page_Load()
If Not IsPostBack Then

Dim PreviousContent As ContentPlaceHolder
PreviousContent = CType(PreviousPage.Controls(0).FindControl("ContentPlaceHolder_Main"), ContentPlaceHolder)

Dim PassedUserName As Label
PassedUserName = CType(PreviousContent.FindControl("lblPassedUserName"), Label)
lblPassedUserName.Text = PassedUserName.Text

Dim ASPNETDB As SqlDataSource = New SqlDataSource()
ASPNETDB.ConnectionString = WebConfigurationManager.ConnectionStrings("ASPNETDB").ConnectionString
ASPNETDB.SelectCommand = "SELECT [UserName], [LastActivityDate] FROM [vw_aspnet_Users] WHERE ([UserName] = @.UserName)"

lblUserName.Text = _____________________________________________________

End If
End Sub

</script>

This link should answer your question.

http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx

Good luck

|||

i guess u need a data adapter to fill the selected data inside a table.....

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 = "your Value";

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!

sql

No comments:

Post a Comment