09-04-2010, 06:40 AM
I was just wondering how can I code this way..
For example I had a button and a textbox and when I click on it... it will connect the database and change the label.text to the information I wan.. which is "either 1 or 0".
Like I have a few user name - "Tom, Dick and Harry"
I enter tom in the textbox and I hit the button. it will load the database and show on the label (Or anything you want).... how many books he rent. actually is either 1 or 0.
I can get it done by using the Gripview control... but I was thinking if there is anyway I can get it done without using it? how can I do a database connection and get information I want without using the gripview data?
as you didn't provide any code...I'll be making some assumptions here to show how you can get this done in asp.net using c#
Let's say you have a deafult.aspx page and it contains the following code within the form's tag
<asp:Label ID="Label1" runat="server" Text="Number of books will be shown here">
<asp:Button ID="btnShowBooks" runat="server" Text="Show Books" onclick="btnShowBooks_Click" />
Here is the code in the button's click event
protected void btnShowBooks_Click(object sender, EventArgs e)
/* get the connection string from the web.config file
if you haven't added one already, here's an example
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|SiteDb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
Please visit this site for connection string examples http://www.connectionstrings.com/
string dbConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection())
connection.ConnectionString = dbConnectionString;
System.Data.SqlClient.SqlCommand cmd = connection.CreateCommand();
// assuming you have a table in your db defined as
// CREATE TABLE [dbo].[Users] ( UserID INT IDENTITY(1,1), Name VARCHAR( 50 ), Books Bit )
cmd.CommandText = "SELECT [Books] FROM dbo.Users WHERE [UserId] = '1'";
System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();
// as we only have one record in the db, we don't need a while
// loop to iterate over the the records in the reader object
// assign the value read from the database to the label
Label1.Text = reader["Books"].ToString();
} //-- ends if
} //-- ends using
} //-- ends btnShowBooks_Click
Hope this helps
09-17-2010, 02:19 PM
First you have to create tables for user id and user name. Now when you start to run the query fetch the username according to userid and store that result in particular variable then display the result on particular label.