Is there Any Error in this code

Posted by Premalatha under C# on 7/9/2012 | Points: 10 | Views : 889 | Status : [Member] | Replies : 6
protected void StudUpdate(object sender, GridViewUpdateEventArgs args)
{
string StudentId = ((Label)GridView1.Rows[args.RowIndex].FindControl("Label1")).Text;
string FirstName = ((Label)GridView1.Rows[args.RowIndex].FindControl("Label2")).Text;
string LastName = ((Label)GridView1.Rows[args.RowIndex].FindControl("Label3")).Text;
string Address = ((Label)GridView1.Rows[args.RowIndex].FindControl("Label4")).Text;
string StudMail = ((Label)GridView1.Rows[args.RowIndex].FindControl("Label5")).Text;
string StudPhone = ((Label)GridView1.Rows[args.RowIndex].FindControl("Label6")).Text;
string Gender = ((Label)GridView1.Rows[args.RowIndex].FindControl("Label7")).Text;
SqlConnection con = new SqlConnection(conStr);

SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
con.Open();
cmd.CommandText = "update Student set FirstName=@p2," + "LastName=@p3," + "Address=@p4," + "StudMail=@p5," + "StudPhone=@p6," + "Gender=@p7 where StudentId=@p1;" + "select StudentId,FirstName,LastName,Address,StudMail,StudPhone,Gender from Student";
cmd.Parameters.Add("@p1", SqlDbType.NChar).Value = StudentId;
cmd.Parameters.Add("@p2", SqlDbType.NVarChar).Value = FirstName;
cmd.Parameters.Add("@p3", SqlDbType.NVarChar).Value = LastName;
cmd.Parameters.Add("@p4", SqlDbType.NVarChar).Value = Address;
cmd.Parameters.Add("@p5", SqlDbType.NVarChar).Value = StudMail;
cmd.Parameters.Add("@p6", SqlDbType.NChar).Value = StudPhone;
cmd.Parameters.Add("@p7", SqlDbType.NVarChar).Value = Gender;
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds,"Student");
GridView1.EditIndex = -1;
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
BindData();
}

Premalatha
Software Engineer



Responses

Posted by: Savari_Arm on: 7/10/2012 [Member] Starter | Points: 25

Up
0
Down
if u face any error?.. what is that

it may help you...

try this add to your code...

string StudentId = ((Label)GridView1.Rows[args.RowIndex].FindControl("Label1")).Text.ToString();
string StudPhone = ((Label)GridView1.Rows[args.RowIndex].FindControl("Label6")).Text.ToString();

Premalatha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Patel28rajendra on: 7/10/2012 [Member] Starter | Points: 25

Up
0
Down
Hi
Premalatha

Are you getting any Error ?
If you got error then post it

Regards

R D Patel

Premalatha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Johnbhatt on: 7/12/2012 [Member] Starter | Points: 25

Up
0
Down
@Premalatha ,

What is the Error that you are getting in above code. There can be so many methods to do same thing.

John Bhatt
Glad to Know, Free to Share.....
http://www.johnbhatt.com

Premalatha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Premalatha on: 7/13/2012 [Member] Starter | Points: 25

Up
0
Down
I am not updating properly.


Premalatha
Software Engineer

Premalatha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 7/13/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
protected void StudUpdate(object sender, GridViewUpdateEventArgs args) 

{
Label StudentId = (Label)GridView1.Rows[args.RowIndex].FindControl("Label1");
Label FirstName = (Label)GridView1.Rows[args.RowIndex].FindControl("Label2");
Label LastName = (Label)GridView1.Rows[args.RowIndex].FindControl("Label3");
Label Address = (Label)GridView1.Rows[args.RowIndex].FindControl("Label4");
Label StudMail = (Label)GridView1.Rows[args.RowIndex].FindControl("Label5");
Label StudPhone = (Label)GridView1.Rows[args.RowIndex].FindControl("Label6");
Label Gender = (Label)GridView1.Rows[args.RowIndex].FindControl("Label7");
SqlConnection con = new SqlConnection(conStr);

SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
con.Open();
cmd.CommandText = "update Student set FirstName=@p2," + "LastName=@p3," + "Address=@p4," + "StudMail=@p5," + "StudPhone=@p6," + "Gender=@p7 where StudentId=@p1;" + "select StudentId,FirstName,LastName,Address,StudMail,StudPhone,Gender from Student";
cmd.Parameters.Add("@p1", SqlDbType.NChar).Value = StudentId.Text;
cmd.Parameters.Add("@p2", SqlDbType.NVarChar).Value = FirstName.Text;
cmd.Parameters.Add("@p3", SqlDbType.NVarChar).Value = LastName.Text;
cmd.Parameters.Add("@p4", SqlDbType.NVarChar).Value = Address.Text;
cmd.Parameters.Add("@p5", SqlDbType.NVarChar).Value = StudMail.Text;
cmd.Parameters.Add("@p6", SqlDbType.NChar).Value = StudPhone.Text;
cmd.Parameters.Add("@p7", SqlDbType.NVarChar).Value = Gender.Text;
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds,"Student");
GridView1.EditIndex = -1;
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
BindData();
}


Premalatha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Patel28rajendra on: 7/13/2012 [Member] Starter | Points: 25

Up
0
Down
Hi
Did you get any solution?


R D Patel

Premalatha, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response