Click here to Skip to main content
11,641,708 members (62,925 online)
Rate this: bad
good
Please Sign up or sign in to vote.
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: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
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 5-Dec-12 21:41pm
Edited 6-Dec-12 19:34pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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: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: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>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

you havnt defined selectedindexchanged method of dropdown list....define it first,in which update your database accordingly....than call databind()....hope this helps....
  Permalink  
Comments
Siddhesh Patankar at 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...
Rohan Leuva at 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
0 DamithSL 233
1 Mika Wendelius 150
2 Sergey Alexandrovich Kryukov 134
3 OriginalGriff 110
4 Afzaal Ahmad Zeeshan 89
0 Mika Wendelius 460
1 OriginalGriff 265
2 DamithSL 258
3 CPallini 169
4 Afzaal Ahmad Zeeshan 149


Advertise | Privacy | Mobile
Web02 | 2.8.150731.1 | Last Updated 13 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100