0

I want to show my cart details on a Button_Click_Event. I'm using a Gridview control of ASP.NET framework. But the following exception occurs:

object reference is not set to a instance of an object.

How can I fix it? Here is my code:

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
    string st = "select * FROM cart_master";
    cmd = new SqlCommand(st, sqlcon);
    cmd.Connection.Open();
    cmd.ExecuteNonQuery();
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    GridView GridView1 = (GridView)ContentPlaceHolder1.FindControl("GridView1");
    sda.Fill(ds, "cart_master");
    GridView1.DataSource = ds.Tables["cart_master"];
    GridView1.DataBind();
    cmd.Connection.Close();
}
honk
  • 9,137
  • 11
  • 75
  • 83
Anurag Dixit
  • 27
  • 1
  • 1
  • 11

1 Answers1

0

Rather than dataset, just fill a datatable from your adapter

DataTable dataTable = new DataTable("ResultDataTable");
sda.Fill( dataTable);
GridView1.DataSource = dataTable;
Pankaj
  • 2,618
  • 3
  • 25
  • 47