Click here to Skip to main content
15,069,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I have a grid view which will update user details. There are 2 dropdowns whose values are not getting updated in the DB instead the original values get deleted and blank values are getting replaced. After debugging the code I find that all values are getting properly sent to respective functions and variables. The other fields like textboxes are getting updated.
This is the code
The HTML
ASP.NET
<asp:GridView ID="grdUsers" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="userid" DataSourceID="dsUsers" EnableModelValidation="True" 
        onrowdatabound="grdUsers_RowDataBound" onrowcommand="grdUsers_RowCommand" 
        onrowediting="grdUsers_RowEditing" onrowupdating="grdUsers_RowUpdating">
        <Columns>
            <asp:TemplateField HeaderText="User Name">
            <EditItemTemplate>
            <asp:TextBox ID="txtgrdUname" runat="server" Text="<%# Bind('username') %>"></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
            <%# Eval("username") %>
            </ItemTemplate>
            </asp:TemplateField>
            
            <asp:TemplateField HeaderText="PIN">
            <EditItemTemplate>
            <asp:TextBox ID="txtgrdPIN" runat="server" Text="<%# Bind('pin') %>" Enabled="false"></asp:TextBox>
            <asp:LinkButton ID="lnkgrdGenerate" runat="server" CommandName="Generate">Generate</asp:LinkButton>
            </EditItemTemplate>
            <ItemTemplate>
            <%# Eval("pin") %>
            </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="E-Mail">
            <EditItemTemplate>
            <asp:TextBox ID="txtgrdEmail" runat="server" Text="<%# Bind('email') %>"></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
            <%# Eval("email") %>
            </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Designation">
            <EditItemTemplate>
            <asp:DropDownList ID="ddlgrdDesg" runat="server" >
            <asp:ListItem>Sales</asp:ListItem>
            <asp:ListItem>Client Service</asp:ListItem>
            <asp:ListItem>Content Writer</asp:ListItem>
            <asp:ListItem>PR</asp:ListItem>
            <asp:ListItem>HR</asp:ListItem>
            <asp:ListItem>Admin</asp:ListItem>
            <asp:ListItem>Network Administrator/Engineer</asp:ListItem>
            <asp:ListItem>Software Developer</asp:ListItem>
            <asp:ListItem>DBA</asp:ListItem>
            <asp:ListItem>Web designer</asp:ListItem>
            <asp:ListItem>Flash Programmer</asp:ListItem>
            <asp:ListItem>Reporting Manager</asp:ListItem>
            </asp:DropDownList>
            </EditItemTemplate>
            <ItemTemplate>
            <%# Eval("designation") %>
            </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="User Type">
            <EditItemTemplate>
                    <asp:DropDownList ID="ddlgrdUType" runat="server">
                             <asp:ListItem>Admin</asp:ListItem>
                             <asp:ListItem>Normal User</asp:ListItem>
                    </asp:DropDownList>
            </EditItemTemplate>
            <ItemTemplate>
            <%# Eval("usertype") %>
            </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField>
            <EditItemTemplate>
            <asp:LinkButton ID="lnkgrdOK" runat="server" CommandName="UPDATE">OK</asp:LinkButton>
            </EditItemTemplate>
            <ItemTemplate>
            <asp:LinkButton ID="lnkgrdEDIT" runat="server" CommandName="EDIT">Edit</asp:LinkButton>
            </ItemTemplate>
            
            </asp:TemplateField>

            <asp:TemplateField>
            <ItemTemplate>
            <asp:LinkButton ID="lnkgrdDelete" runat="server" CommandName="DELETE">Delete</asp:LinkButton>
            </ItemTemplate>
            </asp:TemplateField>
            <%--      <asp:BoundField DataField="pin" HeaderText="pin" SortExpression="pin" />
            <asp:BoundField DataField="email" HeaderText="email" SortExpression="email" />
            <asp:BoundField DataField="designation" HeaderText="designation" 
                SortExpression="designation" />
            <asp:BoundField DataField="usertype" HeaderText="usertype" 
                SortExpression="usertype" />--%>
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="dsUsers" runat="server" 
        ConnectionString="<%$ ConnectionStrings:GloboseMasterConnectionString %>" 
        DeleteCommand="DELETE FROM [Users] WHERE [userid] = @userid" 
        InsertCommand="INSERT INTO [Users] ([username], [pin], [email], [designation], [usertype]) VALUES (@username, @pin, @email, @designation, @usertype)" 
        SelectCommand="SELECT * FROM [Users]" 
        UpdateCommand="UPDATE [Users] SET [username] = @username, [pin] = @pin, [email] = @email, [designation] = @designation, [usertype] = @usertype WHERE [userid] = @userid">
        <DeleteParameters>
            <asp:Parameter Name="userid" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="username" Type="String" />
            <asp:Parameter Name="pin" Type="String" />
            <asp:Parameter Name="email" Type="String" />
            <asp:Parameter Name="designation" Type="String" />
            <asp:Parameter Name="usertype" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="username" Type="String" />
            <asp:Parameter Name="pin" Type="String" />
            <asp:Parameter Name="email" Type="String" />
            <asp:Parameter Name="designation" Type="String" />
            <asp:Parameter Name="usertype" Type="String" />
            <asp:Parameter Name="userid" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>

The C# Code Behind
C#
protected void grdUsers_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        UserBAL user = new UserBAL();
        string uname, pin, designation, usertype,email;
        int id;
        id = Convert.ToInt32(grdUsers.DataKeys[e.RowIndex].Value.ToString());
        uname = Convert.ToString(((TextBox)grdUsers.Rows[e.RowIndex].FindControl("txtgrdUname")).Text.Trim());
        pin = Convert.ToString(((TextBox)grdUsers.Rows[e.RowIndex].FindControl("txtgrdPIN")).Text.Trim());
        email = Convert.ToString(((TextBox)grdUsers.Rows[e.RowIndex].FindControl("txtgrdEmail")).Text.Trim());
        designation = Convert.ToString(((DropDownList)grdUsers.Rows[e.RowIndex].FindControl("ddlgrdDesg")).SelectedItem.Text);
        usertype = Convert.ToString(((DropDownList)grdUsers.Rows[e.RowIndex].FindControl("ddlgrdUType")).SelectedItem.Text);
        
        int result = user.UpdateDetails(id, uname, pin, designation, usertype,email);
        grdUsers.EditIndex = -1;
        grdUsers.DataBind();
    }


    protected void grdUsers_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grdUsers.EditIndex = e.NewEditIndex;
        grdUsers.DataBind();

    }
Posted
Updated 6-Dec-12 19:34pm
v2

I found the problem.
Actually the SQL Data Source Update Query was causing the problem. I just removed the parameters from the Update Command Attribute causing the problem from the query and it worked.
Now I have another problem which i will start a new thread.
The First Code of mine:
ASP.NET
<asp:sqldatasource id="dsUsers" runat="server" xmlns:asp="#unknown">
        ConnectionString="<%$ ConnectionStrings:GloboseMasterConnectionString %>" 
        DeleteCommand="DELETE FROM [Users] WHERE [userid] = @userid" 
        InsertCommand="INSERT INTO [Users] ([username], [pin], [email], [designation], [usertype]) VALUES (@username, @pin, @email, @designation, @usertype)" 
        SelectCommand="SELECT * FROM [Users]" 
        UpdateCommand="UPDATE [Users] SET [username] = @username, [pin] = @pin, [email] = @email, [designation] = @designation, [usertype] = @usertype WHERE [userid] = @userid">
        <deleteparameters>
            <asp:parameter name="userid" type="Int32" />
        </deleteparameters>
        <insertparameters>
            <asp:parameter name="username" type="String" />
            <asp:parameter name="pin" type="String" />
            <asp:parameter name="email" type="String" />
            <asp:parameter name="designation" type="String" />
            <asp:parameter name="usertype" type="String" />
        </insertparameters>
        <updateparameters>
            <asp:parameter name="username" type="String" />
            <asp:parameter name="pin" type="String" />
            <asp:parameter name="email" type="String" />
            <asp:parameter name="designation" type="String" />
            <asp:parameter name="usertype" type="String" />
            <asp:parameter name="userid" type="Int32" />
        </updateparameters>
    </asp:sqldatasource>


Updated Code:

ASP.NET
<asp:sqldatasource id="dsUsers" runat="server" xmlns:asp="#unknown">
       ConnectionString="<%$ ConnectionStrings:GloboseMasterConnectionString %>"
       DeleteCommand="DELETE FROM [Users] WHERE [userid] = @userid"
       InsertCommand="INSERT INTO [Users] ([username], [pin], [email], [designation], [usertype]) VALUES (@username, @pin, @email, @designation, @usertype)"
       SelectCommand="SELECT * FROM [Users]"
       UpdateCommand="UPDATE [Users] SET [username] = @username, [pin] = @pin, [email] = @email WHERE [userid] = @userid">
       <deleteparameters>
           <asp:parameter name="userid" type="Int32" />
       </deleteparameters>
       <insertparameters>
           <asp:parameter name="username" type="String" />
           <asp:parameter name="pin" type="String" />
           <asp:parameter name="email" type="String" />
           <asp:parameter name="designation" type="String" />
           <asp:parameter name="usertype" type="String" />
       </insertparameters>
       <updateparameters>
           <asp:parameter name="username" type="String" />
           <asp:parameter name="pin" type="String" />
           <asp:parameter name="email" type="String" />
           <asp:parameter name="designation" type="String" />
           <asp:parameter name="usertype" type="String" />
           <asp:parameter name="userid" type="Int32" />
       </updateparameters>
   </asp:sqldatasource>
   
you havnt defined selectedindexchanged method of dropdown list....define it first,in which update your database accordingly....than call databind()....hope this helps....
   
Comments
Siddhesh Patankar 6-Dec-12 4:40am
   
u didnt get my question i guess....
actually the data is not getting updated in database.....
Lets say the user clicks on some designation...
when i click on OK it shows blank entry and same gets updated in database...
after debugging i found every value was passing correctly in all the fields...
Thanks7872 6-Dec-12 4:46am
   
remember,selectedindexchanged method is i think the must in the issues like the one you are facing,ok.through that,you first update your dropdownlist value to db,nd than gridview will be updated on button click.if you still facing the same issue than you should check your types and queries properly(minor but major errors)...reply if u still confused....

Regards

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900