how to update through griedview in asp.net

Posted by Shanky11 under ASP.NET on 12/17/2012 | Points: 10 | Views : 681 | Status : [Member] | Replies : 9
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection con = new SqlConnection("data source=shankar-pc\\shankar ; initial catalog=master;user id= sa;password=password@1");
con.Open();

string username = GridView1.DataKeys[e.RowIndex].Values["name"].ToString();
//TextBox txtcity = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtUserName");
TextBox txtName = GridView1.Rows[e.RowIndex].FindControl("txtUserName") as TextBox;
string s = txtName.Text.Trim();
string age = GridView1.DataKeys[e.RowIndex].Values["age"].ToString();
SqlCommand cmd = new SqlCommand("update personaldetails set name='" + txtName.Text.Trim() + "'where age=" + age, con);
cmd.ExecuteNonQuery();
con.Close();
}
-----------------------------this is showing name=system.web.UI.Controls.TextBox
I WANT THAT WHEN I AUTOGNERATE UPDATE BUTTON IN GRDVIEW THE TEXT AFTER EDITING IS DISPLAYED




Responses

Posted by: Ankitsrist on: 12/17/2012 [Member] Starter | Points: 25

Up
0
Down
consider this code.......
SqlCommand cmd = new SqlCommand("update personaldetails set name='" + TextBox1.Text + "' where id='"+ GridView1.SelectedValue.ToString()+"'", myobj.sqlcon);

cmd.ExecuteNonQuery();


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

Posted by: Ankitsrist on: 12/17/2012 [Member] Starter | Points: 25

Up
0
Down
consider this code.......
SqlCommand cmd = new SqlCommand("update personaldetails set name='" + TextBox1.Text + "' where id='"+ GridView1.SelectedValue.ToString()+"'", myobj.sqlcon);

cmd.ExecuteNonQuery();


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

Posted by: Shanky11 on: 12/17/2012 [Member] Starter | Points: 25

Up
0
Down
how can i get textbox1 text it is inside editable template in grdview

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

Posted by: Shanky11 on: 12/17/2012 [Member] Starter | Points: 25

Up
0
Down
This is designing part-----------------------------------
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="491px" PageSize="3" EnableViewState="True" ShowFooter="true" ShowHeader="true"
HeaderStyle-BackColor="Aqua" Height="279px" DataKeyNames="name,age"
onselectedindexchanged="GridView1_SelectedIndexChanged"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
onrowcancelingedit="GridView1_RowCancelingEdit">
<Columns>
<asp:TemplateField HeaderText="Name" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<%# Eval("Name")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtUserName" runat="Server" Text='<%# Eval("Name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtnewUserName" runat="Server"></asp:TextBox>
</FooterTemplate>

<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
</asp:TemplateField>

<asp:TemplateField HeaderText="Age">
<ItemTemplate>
<%#Eval("Age")%>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtUserage" runat="Server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<%#Eval("adress")%>

</ItemTemplate>
<FooterTemplate>
<asp:Button ID="Button1" runat="server" Text="Insert" CommandName="add" />
how can i get update value of text box in .cs page for further updation un Row_Updating event

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

Posted by: Ankitsrist on: 12/18/2012 [Member] Starter | Points: 25

Up
0
Down
ok means u are editing inside the gridview....i generaly get back the value into textbox from where i have inserted into the grid view so there i used this technique

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

Posted by: Shanky11 on: 12/18/2012 [Member] Starter | Points: 25

Up
0
Down
yes i want to update through griedviw row_updating event
name.age,address is column name
when i clk edit cursor inside editable template txtboxname then change here and after that update
how can i get update value of txtbox in some variable in code page to update

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

Posted by: Ankitsrist on: 12/18/2012 [Member] Starter | Points: 25

Up
0
Down
http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html
refer this it will definitely help u

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

Posted by: Shanky11 on: 12/18/2012 [Member] Starter | Points: 25

Up
0
Down
bro i m following this site ...

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

Posted by: Ankitsrist on: 12/18/2012 [Member] Starter | Points: 25

Up
0
Down
ok...now ur application running flawless or not

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

Login to post response