5
Answers

Closing database connection in asp.net C# razor syntax

Hello Everyone,
 
I have built a website in asp.net C# webpages with razor syntax. I have used WebMatrix. My database is IBM db2.
 
I have two questions.
 
The first question is : 
 
I have opened a database connection where "client" is my connection string and is stored in my web.config file.
var db = Database.Open(client); 
I would like to know how to close this database connection. 
----------------------------- 
 
The second question is :
 
The website extension is .cshtml
What are the requirements for the .cshtml extension that i need in my webserver in order to deploy it ?
For instance, i have these installed :

IIS 7.0
.Net Framework 4.0
I would be grateful if you could help me with some answers. Thanks in advance.
 

Answers (5)

0
Photo of Riddhi Valecha
NA 3.1k 186.9k 11y
String strConnString = ConfigurationManager.ConnectionStrings["EMPLOYEEConnectionString2"].ConnectionString;
    SqlConnection con = new SqlConnection(strConnString);
               

You have not assigned the connection string to SQLConnection Object.


0
Photo of Vishal Gilbile
NA 13.9k 2.9m 12y
Hello Friend,
                The issue lies over here.

you access the connection string value from web.config file

   String strConnString = ConfigurationManager.ConnectionStrings["EMPLOYEEConnectionString2"].ConnectionString


You initialized your SqlConnection object

 SqlConnection con = new SqlConnection();

and in the btnSave click event

you have not set the SqlConnection ConnectionString property

what you need to do is inside btnSave Click event

con.ConnectionString=strConnString;

and your issue will be solved.
0
Photo of Riddhi Valecha
NA 3.1k 186.9k 12y
Hi...

Can you share the project/website??

It becomes difficult here to explain.
0
Photo of Jignesh Trivedi
NA 61.3k 14.2m 12y

hi,

this error might due to wrong Connection string in web config.
can you please share it with us.

else verified your config with following example
<connectionStrings>
    <add name="EMPLOYEEConnectionString2" connectionString="Data Source=databaseserverName;user id=sa;password=password;Initial Catalog=databaseName;"/>
  </connectionStrings>

hope this will help you.

0
Photo of Satyapriya Nayak
NA 53k 8m 12y
public partial class j : System.Web.UI.Page
{
// SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EMPLOYEEConnectionString2"].ConnectionString);
int intcount;
SqlCommand cmd;
string sqlstr;
int count;
String strConnString = ConfigurationManager.ConnectionStrings["EMPLOYEEConnectionString2"].ConnectionString;





protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSave_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "JOINMAX";
cmd.Parameters.Add("@NAME", SqlDbType.VarChar).Value = txtName.Text.Trim();
cmd.Parameters.Add("@EMAIL", SqlDbType.VarChar).Value =txtEMAIL.Text.Trim();
cmd.Parameters.Add("@PHONENO", SqlDbType.VarChar).Value =txtPHONE.Text.Trim();
cmd.Parameters.Add("@ERROR", SqlDbType.Char, 500);
cmd.Parameters["@ERROR"].Direction = ParameterDirection.Output;






cmd.Connection = con;
try
{
con.Open();
cmd.ExecuteNonQuery();
string message = (string)cmd.Parameters["@ERROR"].Value;
Label1.Text = "Record inserted successfully";
}
catch (Exception ex)
{
throw ex;
}
finally
{

con.Close();
con.Dispose();
}
0
Photo of gaurav singh
NA 47 14.3k 12y
you can use this code it would be help  to you....


 SqlConnection cn = new SqlConnection();
       cn.ConnectionString = ConfigurationManager.ConnectionStrings["EMPLOYEEConnectionString2"].ConnectionString.ToString();
        cn.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "JOINMAX";
        cmd.Parameters.Add("@NAME", SqlDbType.VarChar).Value = "Gaurav";
        cmd.Parameters.Add("@EMAIL", SqlDbType.VarChar).Value = "@gmail.com";
        cmd.Parameters.Add("@PHONENO", SqlDbType.VarChar).Value = "9711182587";
        cmd.Parameters.Add("@ERROR", SqlDbType.Char, 500);
        cmd.Parameters["@ERROR"].Direction = ParameterDirection.Output;

        cmd.Connection = cn;
        try
        {
           
            cmd.ExecuteNonQuery();
            string message = (string)cmd.Parameters["@ERROR"].Value;
            string msg = "Record inserted successfully";
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {


            cn.Close();
            cn.Dispose();
        }