how to update through row_updating

Posted by Shanky11 under ASP.NET on 12/17/2012 | Points: 10 | Views : 1094 | Status : [Member] | Replies : 16
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");
string nam = GridView1.DataKeys[e.RowIndex].Values["txtcity"].ToString();
string age = GridView1.DataKeys[e.RowIndex].Values["age"].ToString();
SqlCommand cmd = new SqlCommand("update personaldetails set name='" + txtcity.Text + "'where UserId=" + age, con);
cmd.ExecuteNonQuery();
con.Close();
//lblresult.ForeColor = Color.Green;
// lblresult.Text = username + " Details Updated successfully";
GridView1.EditIndex = -1;
BindEmployeeDetails();
--------------------------------------this is not working if anyone knows plese provide code but not any link




Responses

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

Up
0
Down
instead of this....SqlCommand cmd = new SqlCommand("update personaldetails set name='" + txtcity.Text + "'where UserId=" + age, con);

Try this code
SqlCommand cmd = new SqlCommand("update personaldetails set name='" + txtcity.Text + "' where UserId='"+ GridView1.SelectedValue.ToString()+"'", con);

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
what error u are getting???

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
actually my pblm is how to get textbox value in Row_Updating Event to update value in database??????????
txtcity.Text shows System.web.ui.controls
my isssue is when i click autogenerate edit button and edit the value in textbox and update it
4 that i need txtbox value in row_updating event

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

see this is my designing part---------------------------
i want to update name field in database .....how can i get update value of text inside .cs page on row_updating event

<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>
<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" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Delete" ShowDeleteButton="true"
ShowHeader="true" />
<asp:CommandField HeaderText="Edit" ShowEditButton ="true" />
<asp:CommandField HeaderText="Select" ShowSelectButton="true" />

</Columns>
<EmptyDataTemplate>
<asp:Button ID="btnInsert" runat="Server" Text="Insert" CommandName="EmptyInsert" UseSubmitBehavior="False" />
</EmptyDataTemplate>
</asp:GridView>

<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" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Delete" ShowDeleteButton="true"
ShowHeader="true" />
<asp:CommandField HeaderText="Edit" ShowEditButton ="true" />
<asp:CommandField HeaderText="Select" ShowSelectButton="true" />

</Columns>
<EmptyDataTemplate>
<asp:Button ID="btnInsert" runat="Server" Text="Insert" CommandName="EmptyInsert" UseSubmitBehavior="False" />
</EmptyDataTemplate>
</asp:GridView>

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
not getting any error i just want to know that how can i get update value in string to further update in database ????
in grieview edit select,delete button
when i clk edit cursor comes to txtfield then i have to make some changes to value then i colck update
now i want to update this value in db

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
try this in update event handling this is the code which i tried and its working
int userid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());

string username = gvDetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
TextBox txtcity = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtcity");
TextBox txtDesignation = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtDesg");
con.Open();
SqlCommand cmd = new SqlCommand("update Employee_Details set City='" + txtcity.Text + "',Designation='" + txtDesignation.Text + "' where UserId=" + userid, con);
cmd.ExecuteNonQuery();


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
have u got???

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
it shows error in input string was not in correct format


int userid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());
column is name,age,address these r the columns name of my table


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
delete event working?

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
what is the primary key in ur table

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
dont just copy paste that i have mention above replace it with ur used names.....
string username = GridView1.DataKeys[e.RowIndex].Values.ToString();
try this

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
still getting problem????

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
my pblm is that when i click edit it comes to txtbox whch resides inside editable template ok
then i made some changes ................now pblm is that how can i get this updatable content of textbox in code page to run this query
update personaldetails set name='"+name that i have changed in griedview+"' where age =19

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
u have to use it txtUserName.Text because as i have seen in ur design part u have used textboxId= txtUserName this in ur edit item template tag...

use this querySqlCommand cmd = new SqlCommand("update personaldetails set name='" + txtUserName.Text + "' where UserId=" + age, con);



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
this is how u can get the new value which u have entered in the textbox which gets open when u click edit button....

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
try this

 int UserId= Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["age"].ToString());

SqlCommand cmd = new SqlCommand("delete from state where UserId="+age,con);


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

Login to post response