Showing posts with label datalist. Show all posts
Showing posts with label datalist. Show all posts

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

Thursday, February 16, 2012

a problem in sqldatasource

How I can know my sqldatasourse found any record or not I dont want to use datalist or another data table .and how I can put each feild that sqldatasource found on a string object( ineed just one record that sqldatasource found.)
I need a code that on page load automaticly goto another pageAngel

In the data source's Selected handler ther is a param SqlDataSourceStatusEventArgs e, e.AffectedRows will expose how many rows are selected~

But since you don't bind it to a data present control, and you have only one row to select, why not useDataReader? Manipulating data isn't data source's job, but DataSet and DataReader are much better at that~

And to goto another page, the simplest way is Response.Redirect() or Server.Transfer()