views:

2253

answers:

1

I have a gridview that, when in edit mode, I use a dropdown to load all provinces, however, when I change the province and click update, my page crashes...I'm not sending the province parameter to the gridview's updateparameters properly...here's my code, someone please help..

            <asp:GridView runat="server" ID="gvUsers" DataKeyNames="UserID" BackColor="#eeeeee" Width="85%"
                    HorizontalAlign="Center"
                    Font-Bold="False" Font-Names="Verdana"
                    Font-Size="10pt" AutoGenerateColumns="False"
                    OnRowDataBound="gvUsers_RowDataBound" 
                    OnRowDeleting="gvUsers_RowDeleting" >
            <HeaderStyle BackColor="#904601" ForeColor="White" 
                   Font-Bold="True" HorizontalAlign="Left" />
            <SelectedRowStyle BackColor="Yellow" />
            <AlternatingRowStyle BackColor="White" Font-Bold="false" />
                   <Columns>
                         <asp:TemplateField>
                       <ItemTemplate>
                           <asp:LinkButton ID="LinkButton2" 
                             CommandArgument='<%# Eval("UserID") %>' 
                             CommandName="Select" runat="server">
                             Select</asp:LinkButton>
                         </ItemTemplate>     
                         </asp:TemplateField>
                        <asp:BoundField DataField="UserID" Visible="False" />
                        <asp:TemplateField HeaderText="Name">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
                                <asp:Label ID="Label2" runat="server" Text='<%# Eval("LastName") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Delete?">
                         <ItemTemplate>
                           <asp:LinkButton ID="LinkButton1" 
                             CommandArgument='<%# Eval("UserID") %>' 
                             CommandName="Delete" runat="server">
                             Delete</asp:LinkButton>
                         </ItemTemplate>
                       </asp:TemplateField>
                    </Columns> 
                <RowStyle HorizontalAlign="Left" />
              </asp:GridView><br /><br />
              <asp:DetailsView runat="server" ID="dvUser" DataSourceID="SqlDataSource1" AutoGenerateRows="False" Width="85%" DataKeyNames="UserID"
                    HorizontalAlign="Center" AutoGenerateInsertButton="True" AutoGenerateEditButton="True" OnItemInserted="dvUsers_ItemInserted" >
                  <Fields>
                      <asp:BoundField DataField="UserID" Visible="False" />
                      <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                      <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                      <asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="true" SortExpression="UserName" />
                      <asp:TemplateField HeaderText="Password">
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblPassword" Text="●●●●●●●●●"></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox runat="server" ID="txtPassword" TextMode="Password" Text='<%# Bind("Password") %>'>
                            </asp:TextBox>
                        </EditItemTemplate>
                      </asp:TemplateField>
                      <asp:TemplateField HeaderText="Birthdate" SortExpression="Birthdate">
                        <EditItemTemplate>
                            <asp:TextBox runat="server" ID="Birthdate" Text='<%# Bind("Birthdate") %>' ></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label runat="Server" ID="lblBirthdate" Text='<%# Bind("Birthdate") %>'></asp:Label>
                        </ItemTemplate>
                      </asp:TemplateField>
                      <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
                      <asp:BoundField DataField="Apt" HeaderText="Apt" SortExpression="Apt" />
                      <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
                      <asp:TemplateField HeaderText="Province">
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblProvince" Text='<%# Eval("ProvName") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList runat="server" ID="ddlProvince" DataValueField="ProvinceID" DataSourceID="SqlDataSource2" DataTextField="Province"></asp:DropDownList>
                        </EditItemTemplate>
                      </asp:TemplateField>
                      <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" />
                      <asp:BoundField DataField="PhoneNum" HeaderText="PhoneNum" SortExpression="PhoneNum" />
                      <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
                      <asp:CheckBoxField DataField="ynAdminUser" HeaderText="ynAdminUser" SortExpression="ynAdminUser" />
                  </Fields>
            </asp:DetailsView>
            <asp:SqlDataSource ID="SqlDataSource2" 
                    runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                    SelectCommand="SELECT ProvinceID, Province FROM tblProvince"></asp:SqlDataSource>
                    <asp:SqlDataSource ID="SqlDataSource1" 
                    runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                    SelectCommand="SELECT a.UserID, a.FirstName, a.LastName, a.UserName, a.Password, a.Birthdate, a.Address, a.Apt, a.City, a.Province, b.Province as 'ProvName',
                                    a.PostalCode, a.PhoneNum, a.Email, a.ynAdminUser
                                    FROM tblUser a
                                    INNER JOIN tblProvince B ON A.Province = B.ProvinceID
                                    WHERE (a.UserID = @UserID) AND (a.ynDelete = 0)"
                    InsertCommand="INSERT INTO tblUser(FirstName, LastName, UserName, Password, Birthdate, Address, Apt, City, Province, PostalCode, Email, PhoneNum, ynAdminUser, ynDelete) VALUES (@FirstName, @LastName, @UserName, @Password, @Birthdate, @Address, @Apt, @City, @Province, @PostalCode, @Email, @PhoneNum, @ynAdminUser, 0)"
                    UpdateCommand="UPDATE tblUser SET FirstName = @FirstName, LastName = @LastName, Birthdate = @Birthdate, Address = @Address, Apt = @Apt, City = @City, Province = @Province, PostalCode = @PostalCode, PhoneNum = @PhoneNum, Email = @Email, ynAdminUser = @ynAdminUser WHERE (UserID = @UserID)">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="gvUsers" Name="UserID" PropertyName="SelectedValue" Type="Int32" />
                    </SelectParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="FirstName" Type="String" />
                        <asp:Parameter Name="LastName" Type="String" />
                        <asp:Parameter DbType="DateTime" Name="BirthDate" />
                        <asp:Parameter Name="Address" Type="String" />
                        <asp:Parameter Name="Apt" Type="String" />
                        <asp:Parameter Name="City" Type="String" />
                        <asp:ControlParameter Name="Province" ControlID="ddlProvince" PropertyName="SelectedValue" Type="Int32" />
                        <asp:Parameter Name="PhoneNum" Type="String" />
                        <asp:Parameter Name="Email" Type="String" />
                        <asp:Parameter Name="ynAdminUser" Type="Boolean" />
                        <asp:Parameter Name="UserID" Type="Int32" />
                    </UpdateParameters>
                    <InsertParameters>
                        <asp:Parameter Name="FirstName" Type="String" />
                        <asp:Parameter Name="LastName" Type="String" />
                        <asp:Parameter Name="UserName" Type="String" />
                        <asp:Parameter Name="Password" Type="String" />
                        <asp:Parameter DbType="DateTime" Name="Birthdate" />
                        <asp:Parameter Name="Address" Type="String" />
                        <asp:Parameter Name="Apt" Type="String" />
                        <asp:Parameter Name="City" Type="String" />
                        <asp:Parameter Name="Province" Type="Int32" />
                        <asp:Parameter Name="PostalCode" Type="String" />
                        <asp:Parameter Name="Email" Type="String" />
                        <asp:Parameter Name="PhoneNum" Type="String" />
                        <asp:Parameter Name="ynAdminUser" Type="Boolean" />
                    </InsertParameters>
                </asp:SqlDataSource>
A: 

You don't need the control binding:

<asp:ControlParameter Name="Province" ControlID="ddlProvince" PropertyName="SelectedValue" Type="Int32" />

Instead, use a regualar parameter like your others and add the following binding to your drop-down list for the edit mode of the gridview:

SelectedValue=<%#Bind("Province")%>
ScottE
There is no SelecedValue option in the dropdown control...when I tried this (without adding the SelectedValue), it crashed trying to pass NULL to the parameter for province
Mark
wow, my mistake! although it doesn't appear in the intellisense popup, I tried it and it worked...thanks so much
Mark
Was a province actually selected? If not, try adding ConvertEmptyStringToNull="false" to your parameter. SelectedValue is the way you two way bind data from a drop-down list.
ScottE