Hi Guyz, im currently studying asp.net, i need a code that retrive data from SQL database and post it using Label (web from), i dont know how to do it, need help... Thanks in advance !!!!
Hi There,
First of all import sqlclient namespace
using System.Data.SqlClient;
Read data from sql database and set value to Label1 and Label2
protectedvoid Page_Load(object sender,EventArgs e){
// Create database connection
SqlConnection connection =newSqlConnection("YourConnectionString");// Create instance of command
SqlCommand command =newSqlCommand("Select Field1, Fields2 From TableName", connection);// Open database connection
command.Connection.Open();
// Execute command and get datareader
System.Data.SqlClient.SqlDataReader datareader = command.ExecuteReader();// Check whether or not datareader has soemthing
if (datareader.Read()){
Label1.Text = datareader.GetString(0);// get Field1
Label2.Text = datareader.GetString(1);// get Field2
}
// Close database connection
command.Connection.Close();
}
No comments:
Post a Comment