Click here to Skip to main content
15,915,019 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class Gridview : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=SREENI\\SQLEXPRESS;Initial Catalog=TestDB;User ID=sa;Password=sa123");
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack);
        {
            SqlConnection con = new SqlConnection();
            SqlDataAdapter da= new SqlDataAdapter("Select * from tblEmployees",con);
            DataSet ds = new DataSet();
            con.Open();
            da.Fill(ds);
            con.Close();
            GridView1.DataSource=ds.Tables[0];
            GridView1.DataBind();
        }
    }
}



error is


Server Error in '/' Application.

The ConnectionString property has not been initialized.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The ConnectionString property has not been initialized.

Source Error:


Line 19: SqlDataAdapter da= new SqlDataAdapter("Select * from tblEmployees",con);
Line 20: DataSet ds = new DataSet();
Line 21: con.Open();
Line 22: da.Fill(ds);
Line 23: con.Close();
Posted
Updated 27-Jul-14 23:47pm
v3
Comments
u mani 28-Jul-14 5:19am    
check the code in below solution

1 solution

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class Gridview : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=SREENI\\SQLEXPRESS;Initial Catalog=TestDB;User ID=sa;Password=sa123");
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack);
{
con.Open();
SqlDataAdapter da= new SqlDataAdapter("Select * from tblEmployees",con);
DataSet ds = new DataSet();

da.Fill(ds);
con.Close();
GridView1.DataSource=ds.Tables[0];
GridView1.DataBind();
}
}
}


execute this one u will get . .
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900