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();
}
Riddhi ValechaPosted May 13, 2013, 12:47 AM
You have not assigned the connection string to SQLConnection Object.
Vishal GilbilePosted Apr 19, 2013, 4:21 AM
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.
Riddhi ValechaPosted Apr 19, 2013, 4:05 AM
Can you share the project/website??
It becomes difficult here to explain.
Jignesh TrivediPosted Apr 19, 2013, 3:42 AM
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
hope this will help you.
Satyapriya NayakPosted Apr 19, 2013, 3:27 AM
{
// 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();
}
Posted Apr 19, 2013, 3:19 AM
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();
}