Getting started

The point of this site is to demonstrate the use of C# code inside the ASP.net Framework with examples of Data Entry.

The following textbox is used to store data in a SQL Database. The entered text is stored as a String and can be changed to whatever you would like.

Enter Text:

Newer Test

The following code is the Page_Load Event that looks up the value in the database and places it into the lable:

public partial class _Default : Page
{
    private readonly string dbConnection = ConfigurationManager.ConnectionStrings["MYCONNECTIONSTRING"].ConnectionString;
 
    protected void Page_Load(object sender, EventArgs e)
    {
 
       
 
        System.Data.SqlClient.SqlConnection sqlConnection1 =
                new System.Data.SqlClient.SqlConnection(dbConnection);
 
        System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.CommandText = "SELECT Text FROM TextExample";
        cmd.Connection = sqlConnection1;
        try
        {
            sqlConnection1.Open();
            cmd.CommandText = "SELECT Text FROM TextExample";
            String NewText = (String)cmd.ExecuteScalar();
            lblTextExample.Text = NewText;
            sqlConnection1.Close();
        }
        catch
        {
            lblTextExample.Text = "There was an error connecting to the Database";
 
        }
    }
}
                                

The following code is the Button firebtnTextExample_Click Event that updates the Database value and then places it into the lable:

protected void btnTextExample_Click(object sender, EventArgs e)
    {
        System.Data.SqlClient.SqlConnection sqlConnection1 =
               new System.Data.SqlClient.SqlConnection(dbConnection);
 
        System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.CommandText = "Update TextExample Set [Text] = @Text WHERE [ID] = 1";
        cmd.Parameters.AddWithValue("@Text", txtTextExample.Text);
        cmd.Connection = sqlConnection1;
        
        try
        {
            sqlConnection1.Open();
            cmd.ExecuteNonQuery();
 
            cmd.CommandText = "SELECT Text FROM TextExample";
            String NewText = (String)cmd.ExecuteScalar();
            lblTextExample.Text = NewText;
            sqlConnection1.Close();
        }
        catch
        {
            lblTextExample.Text = "There was an error connecting to the Database";
 
        }

     }