-2

Here is my code:

 using System; 
 using System.Web.UI.WebControls; 
 using System.Data.SqlClient; 
 using System.Data; 
 using System.Configuration;       

 namespace EmployeeSys
 {
     public partial class WebForm1 : System.Web.UI.Page
     {
        string query, constr;
        SqlConnection con;
        SqlCommand com; 

     public void connection()
     {
          SqlConnection connstr = new SqlConnection(ConfigurationManager.ConnectionStrings["ContactConnectionString"].ConnectionString);
          //constr = ConfigurationManager.ConnectionStrings["ems"].ToString();
          con = new SqlConnection(constr);
          con.Open();
     }
     protected void Page_Load(object sender, EventArgs e)
     {
         if (IsPostBack == false)
         { 
             gedata();> 
         }
         Label1.Visible = false;
     }
     protected void empsave(object sender, EventArgs e)
     { 
         connection();
         HiddenField1.Value = "Insert";
         query = "EmpEntry";
         com = new SqlCommand(query, con);
         com.CommandType = CommandType.StoredProcedure;
         com.Parameters.AddWithValue("@Action",HiddenField1.Value).ToString();
         com.Parameters.AddWithValue("@FName", TextBox1.Text).ToString();
         com.Parameters.AddWithValue("@MName", TextBox2.Text).ToString();
         com.Parameters.AddWithValue("@LName", TextBox3.Text).ToString();
         int result=  com.ExecuteNonQuery();
         con.Close();
         if (result >= 1)
        {
             Label1.Text = "Records Are Added";             
        }          
     }
     public void gedata()
     {
         connection();
         HiddenField1.Value = "view";
         query = "EmpEntry";
         com = new SqlCommand(query, con);
         com.CommandType = CommandType.StoredProcedure;
         com.Parameters.AddWithValue("@Action", HiddenField1.Value).ToString();
         SqlDataAdapter da = new SqlDataAdapter(com);
         DataSet ds = new DataSet();
         da.Fill(ds);
         GridView1.DataSource = ds;
         GridView1.DataBind();
         con.Close();         
     }
     protected void edit(object sender, GridViewEditEventArgs e)
     {
         GridView1.EditIndex= e.NewEditIndex;
         gedata();

     }
     protected void canceledit(object sender, GridViewCancelEditEventArgs e)
     { 
         GridView1.EditIndex = -1;
         gedata();
     }
     protected void update(object sender, GridViewUpdateEventArgs e)
     {
         connection();
         int id=int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
         HiddenField1.Value = "update";
         query = "EmpEntry";
         com = new SqlCommand(query, con);
         com.CommandType = CommandType.StoredProcedure;
         com.Parameters.AddWithValue("@Action", HiddenField1.Value).ToString();
         com.Parameters.AddWithValue("@FName", ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString());
         com.Parameters.AddWithValue("@MName", ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString());
         com.Parameters.AddWithValue("@LName", ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.ToString());
         com.Parameters.AddWithValue("@id", SqlDbType.Int).Value = id;
         com.ExecuteNonQuery();
         con.Close();
         GridView1.EditIndex = -1;
         gedata();         
     }

     protected void delete(object sender, GridViewDeleteEventArgs e)
     {
         connection();
         int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
         HiddenField1.Value = "Delete";
         query = "EmpEntry";
         com = new SqlCommand(query, con);
         com.CommandType = CommandType.StoredProcedure;
         com.Parameters.AddWithValue("@Action", HiddenField1.Value).ToString();
         com.Parameters.AddWithValue("id", SqlDbType.Int).Value = id;
         com.ExecuteNonQuery();
         con.Close();
         gedata();                       
     }
 } 
}

I already added the connection string in the web.config.

The error:

The ConnectionString property has not been initialized.

on this line:

con.open();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

1
con = new SqlConnection(constr);

With this line, you try to pass an SqlConnection object to your SqlConnection constructor as a parameter. That's wrong.

Pass your connection string to your SqlConnection as a parameter. Like;

string connstr = ConfigurationManager.ConnectionStrings["ContactConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(constr);

Also use using statement to dispose your SqlConnection and SqlCommand objects.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

Here is the mistake:

SqlConnection connstr = new SqlConnection(ConfigurationManager.ConnectionStrings["ContactConnectionString"].ConnectionString);
      //  constr = ConfigurationManager.ConnectionStrings["ems"].ToString();
        con = new SqlConnection(constr);
        con.Open();

You need to pass ConnectionString as string to SqlConnection object or assign it to a string but you are initialzing SqlConnection object

do like this:

con= new SqlConnection(ConfigurationManager.ConnectionStrings["ContactConnectionString"].ConnectionString);


con.Open();

or:

string connstr =ConfigurationManager.ConnectionStrings["ContactConnectionString"].ConnectionString;

con = new SqlConnection(constr);

con.Open();

and Remember don't forget to Close connection after DB Operation using:

con.Close()

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160