Pass Static Sqlconnection String in 2tier Arch using C#

Dotnet4ashu
Posted by Dotnet4ashu under ASP.NET category on | Points: 40 | Views : 1655
Hi Friends,

This is another way that you can create SQL connection in AppCode and call it on .cs pages...........

In AppCode
Create class , then passing connectionstring.........
public SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
public int open()
{
con.Open();
return 0;
}
public int close()
{
con.Close();
return 0;
}

On Cs page,
public partial class Samplepage : System.Web.UI.Page
{
public static SqlCommand cmd; ====For creating static sqlcommand
public static SqlConnection con; ====creating static sqlconnection once again for the purpose of calling functions in app code.....
protected void Page_Load(object sender, EventArgs e)
{

}

public static int callOpen()
{
Connectiion cl = new Connectiion();
cl.open(); // call functions
return 0;
}
public static int callClose()
{
Connectiion cll = new Connectiion();
cll.close(); // call functions
return 0;
}
protected void Button1_Click(object sender, EventArgs e)
{
con = new SqlConnection(); //changes comes here No ConfigurationManager.ConnectionString is here bec we declare it app code...
callOpen();
cmd = new SqlCommand("prcSearch",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@username", txtusename.Text);
cmd.Parameters.AddWithValue("@password", txtpass.Text);
// cmd.ExecuteNonQuery();
callClose();
Response.Redirect("DataListView.aspx");

}
}

Comments or Responses

Posted by: T.Saravanan on: 3/13/2012 Level:Silver | Status: [Member] [MVP] | Points: 10
Hi,

Kindly post your code inside the code tag.

Login to post response