Setup SqlConnection (C#) : SqlConnection « ADO.net Database « ASP.Net






Setup SqlConnection (C#)

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script Language="c#" runat="server">
  void Page_Load()
  {
//    string strConnection = "user id=sa;password=;";
  //  strConnection += "initial catalog=northwind;data source=EWANB;";
    //strConnection += "Connect Timeout=30";
    string strConnection = "Data Source=whsql-v08.prod.mesa1.secureserver.net;Initial Catalog=DB_49907;User ID=java2suser;Password='password';";

    data_src.Text = strConnection;
    SqlConnection objConnection = new SqlConnection(strConnection);
    try
    {
      objConnection.Open();
      con_open.Text="Connection opened successfully.<br />";
      objConnection.Close();
      con_close.Text="Connection closed.<br />";
    }
    catch (Exception e)
    {
      con_open.Text="Connection failed to open.<br />";
      con_close.Text=e.ToString();
    }
  }
</script>
<html>
  <body>
  <h4>Testing the data connection
  <asp:label id="data_src" runat="server"/></h4>
  <asp:label id="con_open" runat="server"/><br />
  <asp:label id="con_close" runat="server"/><br />
  </body>
</html>


           
       








Related examples in the same category

1.Open Sql connection (VB.net)
2.Catch data binding exception (VB.net)
3.Use Trace to debug (VB.net)
4.Display of Data in a Table (Grid) Using SQL Objects (VB.net)