6
Answers

ERROR:The ConnectionString property has not been initialized

Photo of parthiban karnan

parthiban karnan

12y
20.3k
1
I GET THE ERROR:

The ConnectionString property has not been initialized.
==================================================

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;
    SqlConnection con = new SqlConnection();
    



    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        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();
        }

Answers (6)

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();
        }