Posted: 7/27/2011
Hi,
Please help i caanot update Repeater ,and if updated the whole column is updated with the same value.
Webconfig
<connectionStrings> <add name="con" connectionString="Data Source=RINO-PC\SQL;Initial Catalog=empoyee;Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings> <
.ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableEventValidation="false" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title></head><body> <form id="form1" runat="server"> <div> </div> <asp:GridView ID="GridView1" runat="server" onselectedindexchanged="GridView1_SelectedIndexChanged"> <Columns> <asp:ButtonField Text="edit" /> <asp:ButtonField Text="delete" /> </Columns> </asp:GridView> <asp:Repeater ID='r1' runat='server' onitemcommand="r1_ItemCommand" > <HeaderTemplate> <table> <tr> <td> <asp:TextBox ID='t1' BackColor='Beige' runat='server' Text='Name'> </asp:TextBox> </td> <td> <asp:TextBox ID='t2' BackColor='Beige' runat='server' Text='age'> </asp:TextBox> </td> <td> <asp:TextBox ID='t3' BackColor='Beige' runat='server' Text='city'> </asp:TextBox> </td> <td> <asp:TextBox ID='t4' BackColor='Beige' runat='server' Text='salary'> </asp:TextBox> </td> </tr> </table> </HeaderTemplate> <ItemTemplate> <table> <tr> <td> <asp:TextBox ID='t9' runat='server' Text='<%#Eval("id")%>'> </asp:TextBox> </td> <tr> <td> <asp:TextBox ID='t5' runat='server' Text='<%#Eval("emp_name")%>'> </asp:TextBox> </td> <td> <asp:TextBox ID='t6' runat='server' Text='<%#Eval("age")%>'> </asp:TextBox> </td> <td> <asp:TextBox ID='t7' runat='server' Text='<%#Eval("city")%>'> </asp:TextBox> </td> <td> <asp:TextBox ID='t8' runat='server' Text='<%#Eval("salary")%>'> </asp:TextBox> </td> <td> <asp:Button ID ='b1' runat='server' Text='Edit' CommandName="ed" > </asp:Button> </td> <td> <asp:Button ID ='b2' runat='server' Text='Delete' CommandName="de" > </asp:Button> </td> </tr> </table> </ItemTemplate> </asp:Repeater>     <table> <tr> <td> <asp:Label ID='l1' runat='server' text='Name'> </asp:Label> </td> <td> <asp:TextBox ID='t9' runat='server' > </asp:TextBox> </td></tr> <tr> <td> <asp:Label ID='l2' runat='server' text='Age'> </asp:Label> </td> <td> <asp:TextBox ID='t10' runat='server' > </asp:TextBox> </td> </tr> <tr> <td> <asp:Label ID='l3' runat='server' text='City'> </asp:Label> </td> <td> <asp:TextBox ID='t11' runat='server' > </asp:TextBox> </td> </tr><tr> <td> <asp:Label ID='l4' runat='server' text='Salary'> </asp:Label> </td> <td> <asp:TextBox ID='t12' runat='server' > </asp:TextBox> </td> </tr></table><table><tr> <td> <asp:Button ID ='b3' runat='server' Text='New' CommandName="new" CommandArgument="btn"> </asp:Button> </td><td> <asp:Button ID ='b4' runat='server' Text='Save' CommandName="save" CommandArgument="btn2"> </asp:Button> </td></tr></table> </form></body></html>
Code Behind
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Configuration;using System.Data;using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page{ SqlConnection conect; SqlCommand cmd; DataSet ds; SqlDataAdapter da; SqlCommandBuilder cmb; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string str = ConfigurationManager.ConnectionStrings["con"].ConnectionString; conect = new SqlConnection(); conect.ConnectionString = str; cmd = new SqlCommand("select * from emp", conect); ds = new DataSet(); da = new SqlDataAdapter(cmd); cmb = new SqlCommandBuilder(da); conect.Open(); da.Fill(ds, "emp"); r1.DataSource = ds.Tables[0]; r1.DataBind(); conect.Close(); } } public void save() { DataRow rr; rr = ds.Tables["emp"].NewRow(); rr[0] = t9.Text; rr[1] = t11.Text; rr[2] = t12.Text; rr[3] = t10.Text; ds.Tables[0].Rows.Add(rr); conect.Open(); da.Update(ds.Tables["emp"]); r1.DataSource = ds.Tables[0]; r1.DataBind(); conect.Close(); } public void blank() { t9.Text = ""; t10.Text = ""; t11.Text = ""; t12.Text = ""; } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { } static int c; protected void r1_ItemCommand(object source, RepeaterCommandEventArgs e) { int i = e.Item.ItemIndex; if (e.CommandName == "ed" && c==0) { Button b = (Button)r1.Items[i].FindControl("b1"); Button bb = (Button)r1.Items[i].FindControl("b2"); b.Text = "Update"; bb.Text = "Cancel"; c = 1; } else if (c==1) { string str = ConfigurationManager.ConnectionStrings["con"].ConnectionString; conect = new SqlConnection(); conect.ConnectionString = str; TextBox tt1 = (TextBox)r1.Items[i].FindControl("t5"); TextBox tt2 = (TextBox)r1.Items[i].FindControl("t6"); TextBox tt3 = (TextBox)r1.Items[i].FindControl("t7"); TextBox tt4 = (TextBox)r1.Items[i].FindControl("t8"); i = i + 1; cmd=new SqlCommand(); cmd.CommandText = "update emp set emp_name='" + tt1.Text + "',city='" + tt3.Text + "',salary='" + tt4.Text + "',age='" + tt2.Text + "' where id=i"; conect.Open(); cmd.Connection = conect; cmd.ExecuteNonQuery(); conect.Close(); r1.DataSource = ds.Tables["emp"]; r1.DataBind();//bind(); c = 0; } } public void bind() { string str = ConfigurationManager.ConnectionStrings["con"].ConnectionString; conect = new SqlConnection(); conect.ConnectionString = str; cmd = new SqlCommand("select * from emp", conect); ds = new DataSet(); da = new SqlDataAdapter(cmd); cmb = new SqlCommandBuilder(da); conect.Open(); da.Fill(ds, "emp"); cmd.ExecuteNonQuery(); r1.DataSource = ds.Tables[0]; r1.DataBind(); conect.Close(); }}
Database design:
Column name datatype Allow null
Id int YES
EMP_NAME varchar(50) yes
Age int yes
City varchar(50) yes
salary int yes
The code( of ASP.NET, C#, SQL etc..) should be added via Insert Code button in the text editor. Please format your code first.
Thank You for your effort i hav jst figured out the solution
i'll make sure next time to embed the code i INSERT CODE.
ThankYou.
Posted: 4/9/2012
I’d like to scrap some Christmas pics… I think I’ll start right now! :)
-----------------------------
www.buywholesaledvd.com/