Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi i have a grid-view with a template field that contain an image button and u can see it in below screenshot
My Gridview
all right i want to when user click on that image button(Edit Button) user will be redirected to the edit page named : EditUser.aspx and send DataKey of Selected row as a Query string That name : stcode
so i write this code for this job
int selectedRow;
protected void imgBtnEdit_Click(object sender, ImageClickEventArgs e)
{
    GridViewRow row = this.GridStudent.SelectedRow;
    selectedRow = int.Parse(GridStudent.DataKeys[row.RowIndex].Value.ToString());
    Response.Redirect("~/Admin/EditUser.aspx?" +
        "stcode=" + selectedRow);
}


in the MainPage and Write this Code in the EditUser.aspx
protected void Page_Load(object sender, EventArgs e)
{
   usercode = Request.QueryString["stcode"];
   GetData();
}


What I have tried:

private void GetData()
{
    DataTable dt = new DataTable();
    con.Open();
    SqlCommand sqlCmd = new SqlCommand("SELECT count(*) from Student where St_Code='" + usercode + "'", con);
    SqlDataAdapter sqldata = new SqlDataAdapter();
    sqldata.Fill(dt);

    if (dt.Rows.Count > 0)
    {
        lblName.Text = dt.Rows[0]["St_Name"].ToString(); //Where ColumnName is the Field from the DB that you want to display
        lblFamily.Text = dt.Rows[0]["St_Family"].ToString();
        lblStcode.Text = dt.Rows[0]["St_Code"].ToString();
        tbPassword.Text = dt.Rows[0]["St_Password"].ToString();
    }
    con.Close();
}


but everytime it will return stcode=0 :(
i know the problem is the mainpage but i dont know how to solve it
Posted
Updated 29-Oct-17 17:50pm
Comments
FranzBe 29-Oct-17 9:59am    
You fill your datatable with the count of the students only, so there will only be one row with one column in it. The code after if (dt.Rows.Count > 0) makes therefore no sense. Try to exchange the 'count(*)' with '*', the 'sqlCmd' should be passed to the constructor of the SqlDataAdapter, in your code there is no connection between the two.
Member 13019612 29-Oct-17 10:33am    
You are right but my problem still stcode=0

1 solution

try this
protected void imgBtnEdit_Click(object sender, ImageClickEventArgs e)
      {
          GridViewRow row = ((Control)sender).Parent.NamingContainer as GridViewRow;
          string key = GridStudent.DataKeys[row.RowIndex].Value.ToString();
          Response.Redirect("~/Admin/EditUser.aspx?stcode=" +  key);
      }



Formatting the sql Query string is vulnerable to SQL Injection[^] attacks
always use Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]

SqlCommand sqlCmd = new SqlCommand("SELECT *  from Student where St_Code=@code", con);
sqlCmd.Parameters.AddWithValue("@code", usercode);


as per comments its no point of getting the count from the table and trying to populate in the textbox, it will throw column not found error, replace it with the desired columns or just add * to the selection .
 
Share this answer
 
v2

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