Slip 10 - F) Write ASP.Net program to connect to the master database in SQL Server in the Page_Load event. When the connection is established, the message “Connection has been established” should be displayed in a label in the form .

Solution:

Default.aspx

<asp:Label ID="lblMessage" runat="server"></asp:Label>


In the Default.aspx.cs file, add the following code in the Page_Load event:

protected void Page_Load(object sender, EventArgs e)
{
    string connectionString = "Data Source=myServerAddress;Initial Catalog=master;User ID=myUsername;Password=myPassword;";
    SqlConnection connection = new SqlConnection(connectionString);

    try
    {
        connection.Open();
        lblMessage.Text = "Connection has been established";
    }
    catch (Exception ex)
    {
        lblMessage.Text = "Error: " + ex.Message;
    }
    finally
    {
        connection.Close();
    }
}

Post a Comment

0 Comments