开发者

ASP.NET: How do "update" a field in the <EditItemTemplate> that has a null value in the database (SQL)

I am using a FormView control. The allows me to update records in the database. However, when a database field is null I can not update the field on the form. It works fine when the field is not a null value. I am not using any code behind (C#) to bind the data or manipulate the data.

I have read that when there is a null value in the database that there is no record in the "dataset". Can anyone show me how to bind a value in the form when there is a null value previously.

Thanks,

J

...

Total Records

<div style="width:1024px; text-align: justify; overflow:hidden; padding-top:0px">                   
<asp:GridView   ID="DealershipGrid" 
                runat="server" 
                AllowPaging="True" 
                AllowSorting="True" 
                AutoGenerateColumns="False" 
                CellPadding="2" 
                DataKeyNames="Dealership_Id" 
                DataSourceID="DealershipsDS" 
                ForeColor="#333333" 
                GridLines="None" 
                Font-Names="Tahoma" 
                Font-Size="9pt" 
                Font-Bold="False" 
                Width="1024px" PageSize="10">
        <PagerSettings Position="TopAndBottom" />
        <FooterStyle BackColor="#8b1111" Font-Bold="True" ForeColor="White" />
        <RowStyle BackColor="#FFFFFF" ForeColor="#333333" Font-Names="Tahoma" Font-Size="8pt" />
            <Columns>
                <asp:CommandField ShowSelectButton="True" />
                <asp:BoundField 
                        DataField="Dealership_Id" 
                        HeaderText="ID" 
                        InsertVisible="False" 
                        ReadOnly="True" 
                        SortExpression="Dealership_Id"
                        HeaderStyle-Width="3%" 
                        ItemStyle-Width="3%" 
                        FooterStyle-Width="3%" >
                    <FooterStyle Width="3%"></FooterStyle>
                    <HeaderStyle Width="3%" HorizontalAlign="Left"></HeaderStyle>
                    <ItemStyle Width="3%"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField 
                        DataField="Dealership_Name" 
                        HeaderText="Dealership" 
                        SortExpression="Dealership_Name" 
                        HeaderStyle-Width="18%" 
                        ItemStyle-Width="18%" 
                        FooterStyle-Width="18%">
                    <FooterStyle Width="18%"></FooterStyle>     
                    <HeaderStyle Width="18%" HorizontalAlign="Left"></HeaderStyle>
                    <ItemStyle Width="18%"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField 
                        DataField="Dealership_BranchName" 
                        HeaderText="Branch" 
                        SortExpression="Dealership_BranchName" 
                        HeaderStyle-Width="18%" 
                        ItemStyle-Width="18%" 
                        FooterStyle-Width="18%">
                    <FooterStyle Width="18%"></FooterStyle>
                    <HeaderStyle Width="18%" HorizontalAlign="Left"></HeaderStyle>
                    <ItemStyle Width="18%"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField 
                        DataField="Dealership_Phone1" 
                        HeaderText="Phone" 
                        SortExpression="Dealership_Phone1" 
                        HeaderStyle-Width="9%" 
                        ItemStyle-Width="9%" 
                        FooterStyle-Width="9%" >
                    <FooterStyle Width="9%"></FooterStyle>
                    <HeaderStyle Width="9%" HorizontalAlign="Left"></HeaderStyle>
                    <ItemStyle Width="9%"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField 
                        DataField="Dealership_Email1" 
                        HeaderText="Email" 
                        SortExpression="Dealership_Email1" 
                        HeaderStyle-Width="37%" 
                        ItemStyle-Width="37%" 
                        FooterStyle-Width="37%">
                    <FooterStyle Width="37%"></FooterStyle>
                    <HeaderStyle Width="37%" HorizontalAlign="Left"></HeaderStyle>
                    <ItemStyle Width="37%"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField 
                        DataField="Dealership_State" 
                        HeaderText="State" 
                        SortExpression="Dealership_State" 
                        HeaderStyle-Width="5%" 
                        ItemStyle-Width="5%" 
                        FooterStyle-Width="5%" >
                    <FooterStyle Width="5%"></FooterStyle>
                    <HeaderStyle Width="5%" HorizontalAlign="Left"></HeaderStyle>
                    <ItemStyle Width="5%"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField 
                        DataField="Dealership_ZipCode" 
                        HeaderText="Zip Code" 
                        SortExpression="Dealership_ZipCode" 
                        HeaderStyle-Width="6%" 
                        ItemStyle-Width="6%" 
                        FooterStyle-Width="6%" >
                    <FooterStyle Width="6%"></FooterStyle>
                    <HeaderStyle Width="6%" HorizontalAlign="Left"></HeaderStyle>
                    <ItemStyle Width="6%"></ItemStyle>
                </asp:BoundField>
            </Columns>
                <PagerStyle BackColor="White" ForeColor="Maroon" HorizontalAlign="Right" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="False" ForeColor="#333333"  />
                <HeaderStyle BackColor="#AB1414" Font-Bold="True" ForeColor="White" Font-Names="Tahoma" Font-Size="9pt" HorizontalAlign="Left" />
                <EditRowStyle BackColor="#999999" ForeColor="Black" />
                <AlternatingRowStyle BackColor="Silver" ForeColor="Black" />
        </asp:GridView>                    

    ConnectionString="<%$ ConnectionStrings:driveaway2day2 %>" 
    SelectCommand="SELECT [Dealership_Id], [Dealership_Name], [Dealership_BranchName], [Dealership_Phone1], [Dealership_Email1], [Dealership_State], [Dealership_ZipCode] FROM [CAR_Dealership] ORDER BY [Dealership_Name]"  EnableViewState="False" OnSelected="DealershipsDS_Selected" >
</asp:SqlDataSource>                                                          

  Dealership Details  

Dealership ID <%# Eval("Dealership_Id")%> Email 1 <%# Eval("Dealership_Email1")%> Dealership <%# Eval("Dealership_Name")%> Email 2 <%# Eval("Dealership_Email2")%> Branch Name <%# Eval("Dealership_BranchName")%> Email 3 <%# Eval("Dealership_Email3")%> Phone 1 <%# Eval("Dealership_Phone1")%> Address <%# Eval("Dealership_Address")%> Phone 2 <%# Eval("Dealership_Phone2")%> City <%# Eval("Dealership_City")%> Phone 3 <%# Eval("Dealership_Phone3")%> State <%# Eval("Dealership_State")%> Fax <%# Eval("Dealership_Fax")%> Zip Code <%# Eval("Dealership_ZipCode")%>  

        <tr>
             <td colspan="4" style="padding-top:5px;height:15px; background-color:#E2DED6;">&nbsp;</td>              
        </tr>
        <tr>
             <td colspan="4" style="padding-top:5px;height:15px;">&nbsp;</td>               
        </tr>

        <tr>
            <td colspan="2" style="padding-top:5px;">
                <asp:Button ID="btnEdit" runat="Server" CommandName="Edit" Text="Edit" Font-Names="Tahoma" Font-Size="10px" />
                <asp:Button ID="btnInsert" runat="Server" CommandName="New" Text="New" Font-Names="tahoma" Font-Size="10px" />
                <asp:Button ID="btnDelete" runat="Server" CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure to Delete?');" Font-Size="10px" Font-Names="tahoma" />
            </td>
        </tr>
    </table>
 </ItemTemplate>

    <EditItemTemplate>
        <table border="0" width="1024px" style="padding-top:0px;">
            <tr>
                <td height="20px" colspan="4" style="padding-top:0px;">&nbsp; </td>
            </tr>
            <tr>
            <td colspan="4" style="padding-left:3px;height:20px; background-color:#E2DED6; font-family: Tahoma; font-size:12px; color:#000000; font-weight:bold; width:1024px;">Dealership Details</td>
            </tr>
            <tr>
                <td>
                    <table width="800px">
                        <tr>
                            <td colspan="4" style="padding-top:5px;height:5px;">&nbsp;</td>        
                        </tr>
                        <tr>                   
                            <td style="padding-top:5px;" width="100px">Dealership ID</td>
                            <td style="padding-top:5px;" width="300px"><%# Eval("Dealership_Id")%></td>
                            <td style="padding-top:5px;" width="100px">Email 1</td>
                            <td style="padding-top:5px;" width="300px">
                                <asp:TextBox class="aclDetTxt" ID="TextBox1" runat="Server" Text='<%# Bind("Dealership_Email1")%>'>
                                </asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td style="padding-top:5px;" width="100px">Dealership</td>
                            <td style="padding-top:5px;" width="300px">
                                <asp:TextBox class="aclDetTxt" ID="TextBox2" runat="Server" Text='<%# Bind("Dealership_Name")%>'>
                                </asp:TextBox></td>
                            <td style="padding-top:5px;" width="100px">Email 2</td>
                            <td style="padding-top:5px;" width="300px">
                                <asp:TextBox class="aclDetTxt" ID="TextBox3" runat="Server" Text='<%# Bind("Dealership_Email2")%>'>
                                </asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td style="padding-top:5px;" width="100px">Branch Name</td>
                            <td style="padding-top:5px;" width="300px">
                                <asp:TextBox class="aclDetTxt"  ID="TextBox4" runat="Server" Text='<%# Bind("Dealership_BranchName")%>'>
                                </asp:TextBox>
                            </td>
                            <td style="padding-top:5px;" width="100px">Email 3</td>
                            <td style="padding-top:5px;" width="300px">
                                <asp:TextBox class="aclDetTxt" ID="TextBox5"  runat="Server" Text='<%# Bind("Dealership_Email3")%>'>
                                </asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td style="padding-top:5px;" width="100px">Phone 1</td>
                            <td style="padding-top:5px;" width="300px">
                                <asp:TextBox class="aclDetTxt" ID="TextBox6" runat="Server" Text='<%# Bind("Dealership_Phone1")%>'>
                                </asp:TextBox>
                            </td>
                            <td style="padding-top:5px;" width="100px">Address</td>
                            <td style="padding-top:5px;" width="300px">
                                <asp:TextBox class="aclDetTxt" ID="TextBox7" runat="Server" Text='<%# Bind("Dealership_Address")%>'>
                                </asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td style="padding-top:5px;" width="100px">Phone 2</td>
                            <td style="padding-top:5px;" width="300px">
                                <asp:TextBox class="aclDetTxt" ID="TextBox8" runat="Server" Text='<%# Bind("Dealership_Phone2")%>'>
                                </asp:TextBox>
                            </td>
                            <td style="padding-top:5px;" width="100px">City</td>
                            <td style="padding-top:5px;" width="300px">
                                <asp:TextBox class="aclDetTxt" ID="TextBox9" runat="Server" Text='<%# Bind("Dealership_City")%>'>
                                </asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td style="padding-top:5px;" width="100px">Phone 3</td>
                            <td style="padding-top:5px;" width="300px">
                                <asp:TextBox class="aclDetTxt" ID="TextBox10" runat="Server" Text='<%# Bind("Dealership_Phone3")%>'>
                                </asp:TextBox>
                            </td>
                            <td style="padding-top:5px;" width="100px">State</td>
                            <td style="padding-top:5px;" width="300px">
                                <asp:DropDownList class="aclDetDD" ID="myDropDownList" runat="server"   
                                    AppendDataBoundItems="True"  
                                    DataSourceID="StatesDS"   
                                    DataValueField="Dealership_State" 
                                    DataTextField="name"  
                                    SelectedValue='<%#Bind("Dealership_State") %>'>  
                                    <asp:ListItem Value="">Select a State...
                                    </asp:ListItem>  
                                </asp:DropDownList>  
                                <asp:XmlDataSource ID="StatesDS" runat="server" DataFile="~/Ddl/States.xml">
                                </asp:XmlDataSource>                         
                            </td>
                        </tr>
                        <tr>
                            <td style="padding-top:5px;" width="100px">Fax</td>
                            <td style="padding-top:5px;" width="300px">
                                <asp:TextBox class="aclDetTxt" ID="TextBox12" runat="Server" Text='<%# Bind("Dealership_Fax")%>'>
                                </asp:TextBox>
                            </td>
                            <td style="padding-top:5px;" width="100px">Zip Code</td>
                            <td style="padding-top:5px;" width="300px">
                                <asp:TextBox class="aclDetTxt" ID="TextBox13" runat="Server" Text='<%# Bind("Dealership_ZipCode")%>'>
                                </asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="4" style="padding-top:5px;height:5px;">&nbsp;</td>        
                        </tr>                        
                    </tr>
                </table>
            </td>

        <tr>
             <td colspan="4" style="padding-top:5px;height:15px; background-color:#E2DED6;">&nbsp;</td>               
        </tr>
        <tr>
             <td colspan="4" style="padding-top:5px;height:15px;">&nbsp;</td>                
        </tr>
         <asp:RequiredFieldValidator ID="req1" runat="Server" ControlToValidate="TextBox1" ErrorMessage="Email 1 is Required" Text="Enter Correct Information.">
            </asp:RequiredFieldValidator>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="Server" ControlToValidate="TextBox2" ErrorMessage="Dealership is Required" Text="Enter Correct Information.">
            </asp:RequiredFieldValidator>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="Server" ControlToValidate="TextBox6" ErrorMessage="Phone 1 is Required" Text="Enter Correct Information.">
            </asp:RequiredFieldValidator>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="Server" ControlToValidate="TextBox7" ErrorMessage="Address is Required" Text="Enter Correct Information.">
            </asp:RequiredFieldValidator>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="Server" ControlToValidate="TextBox9" ErrorMessage="City is Required" Text="Enter Correct Information.">
            </asp:RequiredFieldValidator>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="Server" ControlToValidate="TextBox13" ErrorMessage="Zip Code is Required" Text="Enter Correct Information.">
            </asp:RequiredFieldValidator>
             <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="Server" ControlToValidate="TextBox4" ErrorMessage="Branch/Location is Required" Text="Enter Correct Information.">
            </asp:RequiredFieldValidator>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="Server" ControlToValidate="myDropDownList" ErrorMessage="State is Required" Text="Enter Correct Information.">
            </asp:RequiredFieldValidator>
        <tr>
            <td colspan="2" style="padding-top:5px;">            
                    <asp:Button ID="btnUpdate" runat="Server" CommandName="Update" Text="Update" Font-Names="tahoma" Font-Size="10px" />
                    <asp:Button ID="Button1" runat="Server" CommandName="Cancel" Text="Cancel" CausesValidation="False" Font-Names="tahoma" Font-Size="10px" />
            </td>
        </tr>
        <asp:ValidationSummary ID="ValidationSummary" runat="Server" ShowMessageBox="true" />
    </table>

 </EditItemTemplate>     

  Dealership Details  

Dealership ID <%# Eval("Dealership_Id")%> Email 1 '> Dealership '> Email 2 '> Branch Name '> Email 3 '> Phone 1 开发者_StackOverflow '> Address '> Phone 2 '> City '> Phone 3 '> State


You might have to set null fields to default values in your dataset.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜