Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Introduction:

In this article I will explain how to send gridview row values to other page and after update that record values return back to the gridview page using asp.net.

Description:

I have a gridview with hyperlink fields here my requirement is if I click on hyperlink of particular row I need to display that particular row values into other page in that page user will edit record values after that if he clicks on update button I need to update that record values and get back to previous home page i.e., gridview page. To achieve this first design the table in database like this

ColumnName
DataType
UserId
Int(set identity property=true)
UserName
varchar(50)
FirstName
varchar(50)
LastName
varchar(50)
Email
Varchar(50)
After completion table creation add some dummy data after that creat new website design the Default.aspx page like this
 

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>PassGridviewRow values </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="gvrecords"  AutoGenerateColumns="false"
HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White" DataKeyNames="UserId">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href ='<%#"UpdateGridviewvalues.aspx?UserId="+DataBinder.Eval(Container.DataItem,"UserId") %>'> <%#Eval("UserName") %>  </a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserName" HeaderText="UserName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="Email" HeaderText="Email" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
After that add these namcespace using System.Data and using System.Data.SqlClient in your codebehind and write the following code


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
}
}
protected void BindGridview()
{
SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB");
con.Open();
SqlCommand cmd = new SqlCommand("select * from UserDetails", con);

SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Close();       
DataSet ds = new DataSet();
da.Fill(ds);
gvrecords.DataSource = ds;
gvrecords.DataBind();
}
Our gridview page is ready if you run this page you will find gridview with hyperlink fields if we click on hyperlink field we need to show that particular record values in another page for that add new page to your website and give name it as UpdateGridviewvalues.aspx because I gave this in hyperlink filed that’s why I am saying that give the same name if you give different name for new form change the form name in hyperlink field also here is the link.


<a href ='<%#"UpdateGridviewvalues.aspx?UserId="+DataBinder.Eval(Container.DataItem,"UserId") %>'> <%#Eval("UserName") %>  </a>
Now design UpdateGridviewvalues.aspx like this


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Update Gridview Row Values</title>
<script type="text/javascript">
function Showalert(username) {
alert(username + ' details updated successfully.');
if (alert) {
window.location = 'Default.aspx';
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td colspan="2" align="center">
<b> Edit User Details</b>
</td>
</tr>
<tr>
<td>
User Name:
</td>
<td>
<asp:Label ID="lblUsername" runat="server"/>
</td>
</tr>
<tr>
<td>
First Name:
</td>
<td>
<asp:TextBox ID="txtfname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Last Name:
</td>
<td>
<asp:TextBox ID="txtlname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Email:
</td>
<td>
<asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnUpdate" runat="server" Text="Update" onclick="btnUpdate_Click" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" onclick="btnCancel_Click"/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
After that add these namcespace using System.Data and using System.Data.SqlClient in your codebehind and write the following code


SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB");
private int userid=0;
protected void Page_Load(object sender, EventArgs e)
{
userid = Convert.ToInt32(Request.QueryString["UserId"].ToString());
if(!IsPostBack)
{
BindControlvalues();   
}
}
private void BindControlvalues()
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from UserDetails where UserId=" + userid, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Close();
DataSet ds = new DataSet();
da.Fill(ds);
lblUsername.Text = ds.Tables[0].Rows[0][1].ToString();
txtfname.Text = ds.Tables[0].Rows[0][2].ToString();
txtlname.Text = ds.Tables[0].Rows[0][3].ToString();
txtemail.Text = ds.Tables[0].Rows[0][4].ToString();
}

protected void btnUpdate_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("update UserDetails set FirstName='" + txtfname.Text + "',LastName='" + txtlname.Text + "',Email='" + txtemail.Text + "' where UserId=" + userid, con);
int result= cmd.ExecuteNonQuery();
con.Close();
if(result==1)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "ShowSuccess", "javascript:Showalert('"+lblUsername.Text+"')", true);
}
}

protected void btnCancel_Click(object sender, EventArgs e)
{
Response.Redirect("~/Default.aspx");
}
Demo

Download sample code attached

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

24 comments :

Amit karmase said...

While running sample program nothing is displayed.
I tried to debug in the default page all lines executed but dataset is having null value .SO NOTHING SHOWN IN RUN TIME

Amit karmase said...

Please help fast .I am new in Asp.
I have made proper changes in con statement of sql.

Suresh Dasari said...

@Amit Karmase......
If your getting empty values means your table doesn not contain any data. Please check it once.......

Amit karmase said...

Thank u ...

Amit karmase said...

i want to try this all this code in ms -ACESS.pl suggest me what changes to be made.

Anonymous said...

sir i want same like this but using checkbox inside the gridview. how to do?

Please teach me sir...

Thank you..

cherry said...

thanks sir...it is working like anything...thank you very much...

Braj Kishor said...

Thank you sir, ..............thank you very much..
i am new in asp.net.....i used it.







ANIL BABU Mandla said...

I have one requirement like this
I have a table CUSTOMER in that CUSTID,NAME,URL columns is thier
CUSTID NAME URL
1 asp www.asp.net
2 weblog www.weblogs.asp.net
3 google www.google.com

My question is I opening perticular url based on CUSTID

I am Passing parameter Like www.Example.com?CUSTID=1 That

corressponding URL data(Ex:www.asp.net) It will open directly in browser

using asp.net........
plz help me ...........
Thank you,
anil

Poove said...

Hi Suresh,
I need one help from you.I'm fresher to asp.net.
I want to hyperlink only 3 rows out of 10 rows in gridview.and that hyperlink should connect to another page .please help me .

colol said...

Hai Suresh, my nickname is colol, what about image, for example either the image path or image bytes? So that user can still upload the same images if no change done to the fileupload textbox.

Anonymous said...

I use above code.It displays first row in the table. If i click second row also. Can you help me sir... But it updates the data in db.

Surbhi Tyagi said...

hi ur article is too helpful for me. but i have a question. i have dropdownlist and radio button on edit page.how to fetch the value of dropdownlist and radiobutton from database.

Sathish Ramamurthi said...

i have problem in databound field,my data is not binding in grid view except tat anchor link? help me

Ranga Maddilati said...

hi,
how to highlight the updated record when i set allowpaging=true in gridview in this example.

Sathya Srinivasan said...

Hi Suresh,
Ca n u give example of same gridview column hyperlink if autogenerated column = true.

towsif nasa said...

i want to do this as a object oriented (oop)way. like i write to write database connection code to like GatewayDB file. how

Anonymous said...

i want to give column name of hyper linked column .. can you please tell me how to give that name?

Mohd Asif said...

thank you very much sir

bobby said...

hi your article is too helpful for me. but i have a question
i have 9 columns(SaleDate DistributorCode NumberofInvoices SaleQty SaleValue ReturnQty ReturnValue GPRAmount TTSAmount ) in that i made DistributorCode as Hyper link. when user clicks that the distributor code, Saledate and the market which i select in the drop down initially needs to go to other page

sagar patel said...

what will be the code if i put BUTTON Instead of "LINK BUTTON" ??

mithun said...

suresh da (anna) ...you helped alot....
mithun...
Tezpur University
Assam

Anonymous said...

chek your demo image so many image not displayed

bharat singh said...

very nice thanks help alot

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.