locked
Problem with updating RRS feed

  • Question

  • User1407015516 posted

    Problem with row_updating Event!!!!!!!!!!!

    protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { SqlConnection con = new SqlConnection("Data Source=STD-258E51EA446\\SQLEXPRESS;Initial Catalog=Customers;Integrated Security=True"); SqlCommand cmd = new SqlCommand("usp_state", con); con.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); cmd.ExecuteScalar(); ddlstate.DataTextField = "StateNames"; ddlstate.DataValueField = "StateID"; ddlstate.DataSource = ds; ddlstate.DataBind(); ddlstate.Items.Insert(0, new ListItem("Select")); con.Close(); SqlConnection con1 = new SqlConnection("Data Source=STD-258E51EA446\\SQLEXPRESS;Initial Catalog=Customers;Integrated Security=True"); SqlCommand cmd1 = new SqlCommand("usp_products", con1); con1.Open(); SqlDataAdapter da1 = new SqlDataAdapter(cmd1); DataSet ds1 = new DataSet(); da1.Fill(ds1); cmd1.ExecuteScalar(); ddlproducts.DataTextField = "Products"; ddlproducts.DataValueField = "ProductID"; ddlproducts.DataSource = ds1; ddlproducts.DataBind(); ddlproducts.Items.Insert(0, new ListItem("Select")); con1.Close(); } bindgrid(); } protected void btnsubmit_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection("Data Source=STD-258E51EA446\\SQLEXPRESS;Initial Catalog=Customers;Integrated Security=True"); SqlCommand cmd = new SqlCommand("usp_insert", con); con.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@CustomerName", txtcustname.Text); cmd.Parameters.AddWithValue("@Address", txtadd.Text); cmd.Parameters.AddWithValue("@City", txtcity.Text); cmd.Parameters.AddWithValue("@Zipcode", txtzip.Text); cmd.Parameters.AddWithValue("@RegistrationDate", txtregdate.Text); cmd.Parameters.AddWithValue("@Email", txtemail.Text); cmd.Parameters.Add("@StateID",SqlDbType.Int).Value = ddlstate.SelectedItem.Value; cmd.Parameters.Add("@ProductID",SqlDbType.Int).Value = ddlproducts.SelectedItem.Value; DataSet ds = new DataSet(); da.Fill(ds); cmd.ExecuteNonQuery(); GridView1.DataSource = ds; GridView1.DataBind(); con.Close(); } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; bindgrid(); } public void bindgrid() { SqlConnection con = new SqlConnection("Data Source=STD-258E51EA446\\SQLEXPRESS;Initial Catalog=Customers;Integrated Security=True"); SqlCommand cmd = new SqlCommand("usp_join", con); con.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); cmd.ExecuteScalar(); GridView1.DataSource = ds; GridView1.DataBind(); con.Close(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; bindgrid(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { SqlConnection con = new SqlConnection("Data Source=STD-258E51EA446\\SQLEXPRESS;Initial Catalog=Customers;Integrated Security=True"); SqlCommand cmd = new SqlCommand("usp_update", con); con.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@CustomerID", SqlDbType.Int); cmd.Parameters.AddWithValue("@CustomerName", SqlDbType.VarChar); cmd.Parameters.AddWithValue("@Address", SqlDbType.VarChar); cmd.Parameters.AddWithValue("@City", SqlDbType.VarChar); cmd.Parameters.AddWithValue("@Zipcode", SqlDbType.VarChar); cmd.Parameters.AddWithValue("@RegistrationDate", SqlDbType.DateTime); cmd.Parameters.AddWithValue("@Email", SqlDbType.VarChar); DataSet ds = new DataSet(); da.Fill(ds); cmd.ExecuteNonQuery(); //GridView1.DataSource = ds; //GridView1.DataBind(); GridView1.EditIndex = -1; bindgrid(); con.Close(); } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; bindgrid(); } } "S.P" [usp_join] SELECT Customer.CustomerName,Customer.Address, State.StateNames,Customer.City,Customer.Zipcode,Customer.Email,Customer.RegistrationDate,Product.Products From Customer Left outer join State on Customer.StateID = State.StateID Left outer join Product on Customer.ProductID = Product.ProductID "S.P" [usp_insert] (@CustomerName varchar(50),@Address varchar(50), @City varchar(50), @Zipcode varchar(6),@RegistrationDate Datetime, @Email varchar(50),@StateID int,@ProductID int) AS insert into Customer (CustomerName ,Address , City , Zipcode ,RegistrationDate , Email ,StateID ,ProductID) values (@CustomerName ,@Address , @City , @Zipcode ,@RegistrationDate , @Email ,@StateID ,@ProductID) "S.P" [usp_update] (@CustomerID int,@CustomerName varchar(50),@Address Varchar(50),@City varchar(50),@Zipcode varchar(50),@Email Varchar(50),@RegistrationDate Datetime) AS update Customer set CustomerName=@CustomerName, Address=@Address, City=@city, Zipcode=@Zipcode, Email=@Email, RegistrationDate=@RegistrationDate where CustomerID=@CustomerID

    Sunday, January 29, 2012 2:55 PM

Answers

  • User3866881 posted

    I tried kind of this but wasnt satisfatory....can i get like this? while clicking edit command field that rows data should be populated in the table i hav created above and edit there itself and update using update command field

    It seems that you don't want to edit in the GridView directly and you want to do it with GridView+DetailsView——

    http://www.codeproject.com/Articles/16779/GridView-DetailsView-Master-Detail-Control

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 3, 2012 3:08 AM

All replies

  • User-1360095595 posted
    What problem??????
    Sunday, January 29, 2012 2:59 PM
  • User1407015516 posted

    not getting updated after clicking update button. what to do??

     <asp:GridView ID="GridView1" runat="server" align="center" Width="90%" 
                    CellPadding="4" AutoGenerateColumns="False" 
                            onrowcancelingedit="GridView1_RowCancelingEdit" 
                            onrowediting="GridView1_RowEditing" ForeColor="#333333" GridLines="None" 
                            AllowPaging="True" AllowSorting="True" onrowupdating="GridView1_RowUpdating" 
                            PageSize="8" onpageindexchanging="GridView1_PageIndexChanging">
                    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                    <Columns>
                    
                        <asp:TemplateField HeaderText="CustomerName">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("CustomerName") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("CustomerName") %>'></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Address">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Address") %>'></asp:TextBox>
                                <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("StateNames") %>'></asp:TextBox>
                                <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("City") %>'></asp:TextBox>
                                <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Zipcode") %>'></asp:TextBox>
                                
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Address") %>'></asp:Label>
                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("StateNames") %>'></asp:Label>
                                <asp:Label ID="Label4" runat="server" Text='<%# Bind("City") %>'></asp:Label>
                                <asp:Label ID="Label5" runat="server" Text='<%# Bind("Zipcode") %>'></asp:Label>
                                
                                 </ItemTemplate>
                            
                            
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Email">
                            <ItemTemplate>
                                <asp:Label ID="Label6" runat="server" Text='<%# Bind("Email") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="RegistrationDate">
                            <ItemTemplate>
                                <asp:Label ID="Label7" runat="server" Text='<%# Bind("RegistrationDate") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("RegistrationDate") %>'></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Products">
                            <ItemTemplate>
                                <asp:Label ID="Label8" runat="server" Text='<%# Bind("Products") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("Products") %>'></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:CommandField ShowEditButton="True" />
                        <asp:CommandField ShowDeleteButton="True" />
                    </Columns>
                    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <AlternatingRowStyle BackColor="White" />
            </asp:GridView>

    Sunday, January 29, 2012 3:27 PM
  • User1219881809 posted

    rookie tiro

    SqlDataAdapter da = new SqlDataAdapter(cmd);

    1. No SqlDataAdapter is required for insert/update and Delete.

    rookie tiro

    cmd.Parameters.AddWithValue("@CustomerID", SqlDbType.Int); cmd.Parameters.AddWithValue("@CustomerName", SqlDbType.VarChar); cmd.Parameters.AddWithValue("@Address", SqlDbType.VarChar); cmd.Parameters.AddWithValue("@City", SqlDbType.VarChar); cmd.Parameters.AddWithValue("@Zipcode", SqlDbType.VarChar); cmd.Parameters.AddWithValue("@RegistrationDate", SqlDbType.DateTime); cmd.Parameters.AddWithValue("@Email", SqlDbType.VarChar);

    2. Suppose zip-code is in a Text Box named "_txtZipCode". No explicit parameter declaration is required. It should be like,

    cmd.Parameters.AddWithValue("@Zipcode", _txtZipCode.Text);

    3.

    rookie tiro

    DataSet ds = new DataSet(); da.Fill(ds);

    No dataset fill is required for update.

    Regards.

    Monday, January 30, 2012 6:51 AM
  • User1407015516 posted

    Done with changes..still doesnt work.

    Inserting data twice into database is solved but UPDATING still doesnt work. Kindly check editing code also

    Monday, January 30, 2012 8:13 AM
  • User1407015516 posted

    I'm getting an error

     

    DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'CustomerID'.

    As i haven't taken CustomerID in StoredProcedure [usp_join] which was autoincremented and i dont want to show in my grid.

    But i had to use it in row_updating event.......which resulted in this error. plz suggest me where i'm going wrong?

    Monday, January 30, 2012 1:33 PM
  • User3866881 posted

    Hello rookie tiro:)

    Maybe there's something logical wrongly with yours……

    1)

    SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds);

    Look at what I've referred above:Since cmd contains a stored procdure that is for update,how can pass this into SqlDataAdapter?SqlDataAdapter requires a sql statement or a SQL procdure that is for selecting (fetching data contents)。

    2)Please use SqlCommand to update first,and then use SqlDataAdapter+Select statement to Fill the DataTable and do binding。

    Reguards!

    Monday, January 30, 2012 8:17 PM
  • User1219881809 posted

    According to your last updated code, You are not using CustomerID to update in database but you used CustomerName.

    I think you have a column in the GridView naming CustomerID. If it is not required, remove it from the design-end.

     

    Regards,

    Monday, January 30, 2012 11:29 PM
  • User1407015516 posted

    How to edit gridview row and update in gridview row itself. But Not to edit in the table and update at table where i have inserted  values.

     

    when i'm clicking edit option in grid its showing textboxes in that particular row of grid.....but....changing values in that textbox is not effecting....instead i have to enter the my values in the TABLE above where i used to INSERT my values.

    I want to edit the values in those textboxes of the row i have selected with edit command field..... rather changing my values in the table above


    <table cellpadding="3" cellspacing="3" width="80%" align="center"> <tr align="center"> <td colspan="2"> <asp:Label ID="lblsidehead" runat="server" Text="ADD CUSTOMER DETAILS" Font-Bold="True" Font-Size="Medium" ForeColor="#FF0066"></asp:Label> </td> </tr> <tr> <td align="right"> <asp:Label ID="lblcustname" runat="server" Text="Customer Name:" Font-Bold="True" ForeColor="#009999"></asp:Label> <asp:RequiredFieldValidator ID="rfvname" runat="server" ControlToValidate="txtcustname" ErrorMessage="Enter Name">*</asp:RequiredFieldValidator> </td> <td > <asp:TextBox ID="txtcustname" runat="server"></asp:TextBox> </td> </tr> <tr> <td align="right"> <asp:Label ID="lbladd" runat="server" Text="Address:" Font-Bold="True" ForeColor="#009999"></asp:Label> <asp:RequiredFieldValidator ID="rfvadd" runat="server" ControlToValidate="txtadd" ErrorMessage="Enter Address">*</asp:RequiredFieldValidator> </td> <td> <asp:TextBox ID="txtadd" runat="server"></asp:TextBox> </td> </tr> <tr> <td align="right"> <asp:Label ID="lblcity" runat="server" Text="City:" Font-Bold="True" ForeColor="#009999"></asp:Label> <asp:RequiredFieldValidator ID="rfvcity" runat="server" ControlToValidate="txtcity" ErrorMessage="Enter City">*</asp:RequiredFieldValidator> </td> <td> <asp:TextBox ID="txtcity" runat="server"></asp:TextBox> </td> </tr> <tr> <td align="right"> <asp:Label ID="lblZip" runat="server" Text="Zipcode:" Font-Bold="True" ForeColor="#009999"></asp:Label> <asp:RequiredFieldValidator ID="rfvzip" runat="server" ControlToValidate="txtzip" EnableViewState="False" ErrorMessage="Enter ZipCode">*</asp:RequiredFieldValidator> </td> <td> <asp:TextBox ID="txtzip" runat="server"></asp:TextBox> </td> </tr> <tr> <td align="right"> <asp:Label ID="lblstate" runat="server" Text="State:" Font-Bold="True" ForeColor="#009999"></asp:Label> <asp:RequiredFieldValidator ID="rfvstate" runat="server" ControlToValidate="ddlstate" ErrorMessage="Select Any Options" InitialValue="Select">*</asp:RequiredFieldValidator> </td> <td> <asp:DropDownList ID="ddlstate" runat="server"> </asp:DropDownList> </td> </tr> <tr> <td align="right"> <asp:Label ID="lblregdate" runat="server" Text="Registration Date:" Font-Bold="True" ForeColor="#009999"></asp:Label> <asp:RequiredFieldValidator ID="rfvregdate" runat="server" ControlToValidate="txtregdate" ErrorMessage="Enter Registration Date">*</asp:RequiredFieldValidator> </td> <td> <asp:TextBox ID="txtregdate" runat="server"></asp:TextBox> </td> </tr> <tr> <td align="right"> <asp:Label ID="lblEmail" runat="server" Text="Email:" Font-Bold="True" ForeColor="#009999"></asp:Label> <asp:RequiredFieldValidator ID="rfvemail" runat="server" ControlToValidate="txtemail" ErrorMessage="Enter Email">*</asp:RequiredFieldValidator> </td> <td> <asp:TextBox ID="txtemail" runat="server"></asp:TextBox> </td> </tr> <tr> <td align="right"> <asp:Label ID="lblproducts" runat="server" Text="Products:" Font-Bold="True" ForeColor="#009999"></asp:Label> <asp:RequiredFieldValidator ID="rfvproducts" runat="server" ControlToValidate="ddlproducts" ErrorMessage="Select Any Product" InitialValue="Select">*</asp:RequiredFieldValidator> </td> <td> <asp:DropDownList ID="ddlproducts" runat="server"> </asp:DropDownList> </td> </tr> <tr align="center"> <td colspan="2"> <asp:Button ID="btnsubmit" runat="server" Text="SUBMIT" BackColor="Blue" Font-Bold="True" ForeColor="White" onclick="btnsubmit_Click" /> <asp:GridView ID="GridView1" runat="server" align="center" Width="90%" CellPadding="4" AutoGenerateColumns="False" onrowcancelingedit="GridView1_RowCancelingEdit" onrowediting="GridView1_RowEditing" ForeColor="#333333" GridLines="None" AllowPaging="True" AllowSorting="True" onrowupdating="GridView1_RowUpdating" PageSize="8" onpageindexchanging="GridView1_PageIndexChanging" DataKeyNames="CustomerID"> <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <RowStyle BackColor="#FFFBD6" ForeColor="#333333" /> <Columns> <asp:TemplateField HeaderText="CustomerName"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("CustomerName") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("CustomerName") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Address"> <EditItemTemplate> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Address") %>'></asp:TextBox> <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("StateNames") %>'></asp:TextBox> <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("City") %>'></asp:TextBox> <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Zipcode") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("Address") %>'></asp:Label> <asp:Label ID="Label3" runat="server" Text='<%# Bind("StateNames") %>'></asp:Label> <asp:Label ID="Label4" runat="server" Text='<%# Bind("City") %>'></asp:Label> <asp:Label ID="Label5" runat="server" Text='<%# Bind("Zipcode") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Email"> <ItemTemplate> <asp:Label ID="Label6" runat="server" Text='<%# Bind("Email") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="RegistrationDate"> <ItemTemplate> <asp:Label ID="Label7" runat="server" Text='<%# Bind("RegistrationDate") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("RegistrationDate") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Products"> <ItemTemplate> <asp:Label ID="Label8" runat="server" Text='<%# Bind("Products") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("Products") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:CommandField ShowEditButton="True" /> <asp:CommandField ShowDeleteButton="True" /> </Columns> <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /> <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView> </td> </tr> </table>

    Wednesday, February 1, 2012 11:33 PM
  • User3866881 posted

    I want to edit the values in those textboxes of the row i have selected with edit command field..... rather changing my values in the table above

    A better way for you at present is to use SqlDataSource first to bind to the existing table,and then auto-generate inserting, deleting and updating sql commands for your specific table and then Enable Editing and Deleting for the GridView。

    Reguards!

    Thursday, February 2, 2012 12:18 AM
  • User1407015516 posted

    I tried kind of this but wasnt satisfatory....can i get like this? while clicking edit command field that rows data should be populated in the table i hav created above and edit there itself and update using update command field

    Friday, February 3, 2012 1:58 AM
  • User3866881 posted

    I tried kind of this but wasnt satisfatory....can i get like this? while clicking edit command field that rows data should be populated in the table i hav created above and edit there itself and update using update command field

    It seems that you don't want to edit in the GridView directly and you want to do it with GridView+DetailsView——

    http://www.codeproject.com/Articles/16779/GridView-DetailsView-Master-Detail-Control

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 3, 2012 3:08 AM
  • User581622694 posted

    check following update , delete all operation in following example.Its working fine .

    http://hamidseta.blogspot.in/2012/01/sorting-paging-add-update-delete-in.html

    Friday, February 3, 2012 7:50 AM