What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 64968 |  Welcome, Guest!   Register  Login
 Home > Forums > C# > Is there Any Error in this code ...
Premalatha

Is there Any Error in this code

Replies: 6 | Posted by: Premalatha on 7/9/2012 | Category: C# Forums | Views: 282 | Status: [Member] | Points: 10  


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


Reply | Reply with attachment | Alert Moderator

 Responses below this adGet hundreds of .NET Tips and Tricks videos

 Replies

Savari_Arm
Savari_Arm  
Posted on: 7/10/2012 12:35:56 AM
Level: Starter | Status: [Member] | Points: 25

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. | Reply | Alert Moderator 

Patel28rajendra
Patel28rajendra  
Posted on: 7/10/2012 2:26:04 AM
Level: Starter | Status: [Member] | Points: 25

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. | Reply | Alert Moderator 

Johnbhatt
Johnbhatt  
Posted on: 7/12/2012 8:18:03 AM
Level: Starter | Status: [Member] | Points: 25

@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. | Reply | Alert Moderator 

Premalatha
Premalatha  
Posted on: 7/13/2012 12:42:18 AM
Level: Starter | Status: [Member] | Points: 25

I am not updating properly.


Premalatha
Software Engineer

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

Ranjeet_8
Ranjeet_8  
Posted on: 7/13/2012 6:27:53 AM
Level: Gold | Status: [Member] | Points: 25

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. | Reply | Alert Moderator 

Patel28rajendra
Patel28rajendra  
Posted on: 7/13/2012 2:21:21 PM
Level: Starter | Status: [Member] | Points: 25

Hi
Did you get any solution?


R D Patel

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

Reply - Please login to reply


Click here to login & reply

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2013 8:03:45 AM