Click here to Skip to main content
15,901,284 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
When i click on the record to update it. its always send the first record to the form if i click the edit button with the last record it also send the first record to form for editing
protected void GView_RowEditing(object sender, GridViewEditEventArgs e)
       {
           Form_Enable();
 
           GView.EditIndex = e.NewEditIndex;
           string conStr = ConfigurationManager.ConnectionStrings["CStringCRM"].ToString();
           using (SqlConnection conn = new SqlConnection(conStr))
 

           using (SqlCommand cmd = new SqlCommand(@"SELECT * FROM UserInfo WHERE UID = UID", conn))
           {
               conn.Open();
               using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
               {
                   if (dr.Read())
                   {
                       txtUID.Text = Convert.ToString(dr["UID"]);
                       txtFName.Text = Convert.ToString(dr["UFName"]); txtMName.Text = Convert.ToString(dr["UMName"]);
                       txtLName.Text = Convert.ToString(dr["ULName"]); txtUNic.Text = Convert.ToString(dr["UNic"]);
                       txtUDob.Text = Convert.ToString(dr["UDOB"]); txtUHPhone.Text = Convert.ToString(dr["UHPhone"]);
                       txtUCNumber.Text = Convert.ToString(dr["UCPhone"]); txtUEmail.Text = Convert.ToString(dr["UEmail"]);
                       ddlUState.Text = Convert.ToString(dr["UState"]);
                       txtUCity.Text = Convert.ToString(dr["UCity"]);
                       txtUAddress.Text = Convert.ToString(dr["UAddress"]);
                       // txtUComName.Enabled = true; txtUDesignation.Enabled = true; txtWPhone.Enabled = true; txtFaxNumber.Enabled = true; txtBCity.Enabled = true; txtCAddress.Enabled = true;
                       // txtECName.Enabled = true; txtECMNumber.Enabled = true; txtRelationship.Enabled = true; txtRHPhone.Enabled = true; txtECMNumber.Enabled = true;
                       //ddlUState.Text = Convert.ToString(dr["UState"]);
                       DateTime GV_date = DateTime.Now;
                       txtUDob.Text = GV_date.ToShortDateString();
                       this.GView.Visible = false;
                       txtFName.Focus();
                       this.btnUpdate.Enabled = true; this.btnShow.Enabled = false; this.btnSave.Enabled = false; this.btnAdd.Enabled = false; this.btnCancel.Enabled = true; this.btnClear.Enabled = false;
                   }
 
               }
 
           }

// End of Assigning Value to the foreign key of the Business Info and Emrg Contact Info Tables
}
Posted
Updated 3-Aug-15 18:45pm
v2
Comments
[no name] 3-Aug-15 8:45am    
Could it be that the first record is all you are using? I think you would benefit from getting a book on basic .NET programming and work through it.
Altaful Haq 3-Aug-15 8:49am    
if i click the second record in grid view is load the first record into the form...
[no name] 3-Aug-15 8:58am    
Could it be that the first record is all you are using? I think you would benefit from getting a book on basic .NET programming and work through it.

This is because you select every record from the db using "SELECT * FROM table." You then call .Read() which reads in the first record.

As Wes mentioned, you may benefit more from doing some online tutorials or books first.
 
Share this answer
 
Comments
Altaful Haq 4-Aug-15 5:26am    
ya i know i am not export of asp but i stuck with it. and i just need help to solve my problem. if you can just correct my mistakes in the code and suggest your way of going so @RYAN_DEV
ZurdoDev 4-Aug-15 7:02am    
Your sql where clause is wrong. You have "SELECT * FROM UserInfo WHERE UID = UID" That's like saying where 1 = 1. UID = UID

You need something like "SELECT * FROM UserInfo WHERE UID = @UID" and use a parameter. Then do cmd.Parameters.AddWithValue("@UID", uidVariable);
Altaful Haq 4-Aug-15 7:34am    
i tried to follow you but when i do so then the record is not loading into the textboxs in the control. the textboxs and the gridview are an the same control...

Try this and let me know if it does not work


protected void GView_RowEditing(object sender, GridViewEditEventArgs e)
{
Form_Enable();


GView.EditIndex = e.NewEditIndex;


/provide the index of UID column in your gridview for cell array/
String UID= GView.Rows[e.NewEditIndex].Cells[1].Text;
String cmdStr = String.Format("Select * from UserInfo Where UserID = '{0}' ", UID);
string conStr = ConfigurationManager.ConnectionStrings["CStringCRM"].ToString();
using (SqlConnection conn = new SqlConnection(conStr))


using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
{
conn.Open();
using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
if (dr.Read())
{
txtUID.Text = Convert.ToString(dr["UID"]);
txtFName.Text = Convert.ToString(dr["UFName"]); txtMName.Text = Convert.ToString(dr["UMName"]);
txtLName.Text = Convert.ToString(dr["ULName"]); txtUNic.Text = Convert.ToString(dr["UNic"]);
txtUDob.Text = Convert.ToString(dr["UDOB"]); txtUHPhone.Text = Convert.ToString(dr["UHPhone"]);
txtUCNumber.Text = Convert.ToString(dr["UCPhone"]); txtUEmail.Text = Convert.ToString(dr["UEmail"]);
ddlUState.Text = Convert.ToString(dr["UState"]);
txtUCity.Text = Convert.ToString(dr["UCity"]);
txtUAddress.Text = Convert.ToString(dr["UAddress"]);
// txtUComName.Enabled = true; txtUDesignation.Enabled = true; txtWPhone.Enabled = true; txtFaxNumber.Enabled = true; txtBCity.Enabled = true; txtCAddress.Enabled = true;
// txtECName.Enabled = true; txtECMNumber.Enabled = true; txtRelationship.Enabled = true; txtRHPhone.Enabled = true; txtECMNumber.Enabled = true;
//ddlUState.Text = Convert.ToString(dr["UState"]);
DateTime GV_date = DateTime.Now;
txtUDob.Text = GV_date.ToShortDateString();
this.GView.Visible = false;
txtFName.Focus();
this.btnUpdate.Enabled = true; this.btnShow.Enabled = false; this.btnSave.Enabled = false; this.btnAdd.Enabled = false; this.btnCancel.Enabled = true; this.btnClear.Enabled = false;
}


}

}

 
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