Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
See more:
Great day!

Hi everyone. I will insert data in gridview but everytime I click the ADD button when the NAME is empty there is an "Not allow nulls" and the user become clueless if this happened.. So I want to make an error message everytime I click the ADD button when the textboxes are empty..

ASP.NET
<pre><asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" DataSourceID="SqlDataSource1" Height="92px" Width="835px" AllowPaging="True" CellPadding="3" DefaultMode="Insert" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" GridLines="Horizontal" BackImageUrl= "howto.jpg" style="margin-bottom: 12px">
                <EditRowStyle BackColor="#738A9C" BorderStyle="Solid" Font-Bold="True" ForeColor="#F7F7F7" />
                <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                <InsertItemTemplate>
                    <asp:Label ID="lblName" runat="server" Text="Name of Applicant" Font-Size ="10pt" Font-Names="Arial" ></asp:Label>
                                                             
                    <asp:Label ID="lblBday" runat="server" Font-Names="Arial" Font-Size="10pt" Text="Birthday"></asp:Label>
                                                                             
                    <asp:Label ID="Gender" runat="server" Font-Names="Arial" Font-Size="10pt" Text="Gender"></asp:Label>
                                     
                    <br />
                      
                    <asp:TextBox ID="txtName" runat="server" Height="19px" Text='<%# Bind("Name") %>' Width="216px" Font-Size ="10" Font-Names="Arial"></asp:TextBox>
                               
                    <asp:TextBox ID="txtBday" runat="server" Font-Names="Arial" Font-Size="10pt" Height="19px" Text='<%# Bind("Birthday") %>' TextMode="Date" Width="216px"></asp:TextBox>
                             
                    <asp:DropDownList ID="txtGender" runat="server" Height="19px" Width="222px" SelectedValue='<%# Bind("Gender") %>'>
                        <asp:ListItem>Male</asp:ListItem>
                        <asp:ListItem>Female</asp:ListItem>
                    </asp:DropDownList>
                    <br />
                    <br />
                    <asp:Label ID="lblAddress" runat="server" Font-Names="Arial" Font-Size="10pt" Text="Address"></asp:Label>
                                                                                                                                              
                    <asp:Label ID="lblContact" runat="server" Font-Names="Arial" Font-Size="10pt" Text="Contact"></asp:Label>
                    <br />
                      
                    <asp:TextBox ID="txtAddress" runat="server" Font-Names="Arial" Font-Size="10pt" Height="19px" Text='<%# Bind("Address") %>' Width="488px"></asp:TextBox>
                             
                    <asp:TextBox ID="txtContact" runat="server" Font-Names="Arial" Font-Size="10pt" Height="19px" Text='<%# Bind("Contact") %>' Width="216px"></asp:TextBox>
                    <br />
                    <br />
                    <br />
                    <asp:Label ID="lblEduc" runat="server" Font-Names="Arial" Font-Size="10pt" Text="Educational Attainment"></asp:Label>
                                                                                                                         <asp:Label ID="lblCourse" runat="server" Font-Names="Arial" Font-Size="10pt" Text="Course"></asp:Label>
                    <br />
                      
                    <asp:DropDownList ID="txtEduc" runat="server" Height="19px" SelectedValue='<%# Bind("Education") %>' Width="495px">
                        <asp:ListItem>High School</asp:ListItem>
                        <asp:ListItem>College Diploma (2-3 years)</asp:ListItem>
                        <asp:ListItem>College Undergrad (4-5 years)</asp:ListItem>
                        <asp:ListItem>Bachelor's Degree</asp:ListItem>
                        <asp:ListItem Value="Master">Masteral</asp:ListItem>
                        <asp:ListItem>Doctor</asp:ListItem>
                    </asp:DropDownList>
                             
                    <asp:TextBox ID="txtCourse" runat="server" Font-Names="Arial" Font-Size="10pt" Height="19px" Text='<%# Bind("Course") %>' Width="216px"></asp:TextBox>
                    <br />
                    <br />
                    <br />
                                                                                                                                                                            
                  <asp:Button ID="btnAdd" runat="server" Text="ADD" Font-Bold="True" Font-Size="Medium" ForeColor="Black" BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" CausesValidation="True" ValidationGroup="Insert" Font-Names="Berlin Sans FB Demi" CommandName="Insert" />
                         <asp:Button ID="btnCancel" runat="server" Text="CANCEL" Font-Bold="True" Font-Names="Berlin Sans FB Demi" Font-Size="Medium" ForeColor="Black" BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" CommandName="Cancel" />

                </InsertItemTemplate>
                <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
                <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
            </asp:FormView>





What I have tried:

I tried the traditional error handling like

If txtName.text="" Then

'error msg

End if

But it doesn't work. Please Help me thanks!
Posted
Updated 21-May-17 13:08pm
v2
Comments
[no name] 21-May-17 7:44am    
"it doesn't work", if you mean nothing happens then you are correct. You kind of have to write code to actually do something instead of just having a comment inside your code and expecting magic to handle things for you.
Member 12905345 21-May-17 8:28am    
What I mean sir is I tried

If txtName.Text="" Then

Msgbox("Please fill up the form")

End if

Sorry for that.

Use Required Field Validator

<asp:TemplateField HeaderText="Price">
            <ItemTemplate>
                <asp:TextBox ID="txtPrice" runat="server" Text='<%# Eval("Price") %>'></asp:TextBox><br />
                <asp:RequiredFieldValidator ID="rfvPrice" ControlToValidate="txtPrice" runat="server"
                    ErrorMessage="Required" ForeColor="Red"></asp:RequiredFieldValidator>
            </ItemTemplate>
        </asp:TemplateField>
 
Share this answer
 
I also add this code in my Web.config

HTML
<pre><appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
 
Share this answer
 

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



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