简体   繁体   中英

Gridview select row to next page C#

I need to fetch the selected row of Gridview in next page .. I'm getting exception at if (condition) that cannot find table 0 so below is my code.help me out.

protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(cs);
        String abc = "Select fname,lname,city,address,empid,email,phone,department from employee Where id=" + Request.QueryString["a"];
        SqlCommand cmd = new SqlCommand(abc, con);
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        DataSet ds = new DataSet();
        **if (ds.Tables[0].Rows.Count > 0)**   //getting error at this
        {
            fname.Text = ds.Tables[0].Rows[0]["fname"].ToString();
            lname.Text = ds.Tables[0].Rows[0]["lname"].ToString();
            ddl1.SelectedItem.Value = ds.Tables[0].Rows[0]["city"].ToString();
            address.Text = ds.Tables[0].Rows[0]["address"].ToString();
            empid.Text = ds.Tables[0].Rows[0]["empid"].ToString();
            email.Text = ds.Tables[0].Rows[0]["email"].ToString();
            phone.Text = ds.Tables[0].Rows[0]["phone"].ToString();
            DropDownList1.SelectedItem.Value = ds.Tables[0].Rows[0]["department"].ToString();


        }
    }

Before the line on error You need to fill your dataset

da.Fill(ds, "MyTable")

EDIT :

You also didn't open your connection

SqlConnection con = new SqlConnection(cs);
//Open the connection
con.Open()
String abc = "Select fname,lname,city,address,empid,email,phone,department from employee Where id=" + Request.QueryString["a"];
SqlCommand cmd = new SqlCommand(abc, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
//Fill the dataset
da.Fill(ds, "MyTable")
//Close the connection
con.Close()

...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM