RequiredFieldValidator not preventing database from getting updated with an empty row - Visual Studio 2012

StackOverflow https://stackoverflow.com/questions/17078423

  •  31-05-2022
  •  | 
  •  

Question

I'm just playing around with Visual Studio 2012 and trying to make an very simple newssite with an local SQL Server database. The problem is that the RequiredFieldValidator is not working on the textboxes. Even if the boxes are empty the database still gets updated with an empty record and i just dont know why as the validator was working fine in VS2010. If i press the submit button the errors are showing up but the database still getting updated with an empty record. Anyone have a clue why? I'm using ScriptResourceMapping in the C# file of the form if that is making any difference.

Here's my code:

 <section id="addnews_panel">
        <h2>Rubrik</h2>
        <asp:TextBox ID="txtHeadlineForm1" runat="server" Width="332px" CausesValidation="True" ValidationGroup="newsForm"></asp:TextBox>
        <br />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtHeadlineForm1" ErrorMessage="Write something" ValidationGroup="newsForm"></asp:RequiredFieldValidator>
        <h2>Rubrik 2</h2>
        <asp:TextBox ID="txtHeadlineForm2" runat="server" Width="328px" CausesValidation="True" ValidationGroup="newsForm"></asp:TextBox>
        <br />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtHeadlineForm2" ErrorMessage="Write something" ValidationGroup="newsForm"></asp:RequiredFieldValidator>
        <h2>Ingress</h2>
        <asp:TextBox ID="txtIngressForm" runat="server" Height="99px" TextMode="MultiLine" Width="325px" CausesValidation="True" ValidationGroup="newsForm"></asp:TextBox>
        <br />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtIngressForm" ErrorMessage="Write something" ValidationGroup="newsForm"></asp:RequiredFieldValidator>
        <h2>Nyhet</h2>
        <asp:TextBox ID="txtNewsForm" runat="server" Height="288px" TextMode="MultiLine" Width="507px" CausesValidation="True" ValidationGroup="newsForm"></asp:TextBox>
        <br />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtNewsForm" ErrorMessage="Write something" ValidationGroup="newsForm"></asp:RequiredFieldValidator>

        <br />
        <asp:Button ID="btnAddNews" runat="server" Text="Lägg till nyhet" OnClick="btnAddNews_Click" ValidationGroup="newsForm" />

        <h2>

        <asp:Label ID="lblResult" runat="server"></asp:Label>

        </h2>

    </section>



protected void btnAddNews_Click(object sender, EventArgs e)
    {

            using (dbnewsEntities db = new dbnewsEntities())
            {
                var addNews = new tblNews();
                addNews.headline = txtHeadlineForm1.Text;
                addNews.headline2 = txtHeadlineForm2.Text;
                addNews.ingress = txtIngressForm.Text;
                addNews.news = txtNewsForm.Text;
                addNews.date = DateTime.Now.ToString();
                db.tblNews.Add(addNews);
                db.SaveChanges();
            }

            lblResult.Text = "News added!";



    }
Was it helpful?

Solution

You must always validate on the server-side, in case client-side validation is bypassed (i.e. JavaScript is turned off). To validate server-side, try this:

protected void btnAddNews_Click(object sender, EventArgs e)
{
    // Only save to the database and update UI if the page is valid
    if (Page.IsValid)
    {
        using (dbnewsEntities db = new dbnewsEntities())
        {
            var addNews = new tblNews();
            addNews.headline = txtHeadlineForm1.Text;
            addNews.headline2 = txtHeadlineForm2.Text;
            addNews.ingress = txtIngressForm.Text;
            addNews.news = txtNewsForm.Text;
            addNews.date = DateTime.Now.ToString();
            db.tblNews.Add(addNews);
            db.SaveChanges();
        }

        lblResult.Text = "Nyhet sparad!";
    }
}

Another approach is to bail out of the method if you find that page validation has failed, like this:

protected void btnAddNews_Click(object sender, EventArgs e)
{
    // If the page is invalid, then return from the method
    if (!Page.IsValid)
    {
        return;
    }

    using (dbnewsEntities db = new dbnewsEntities())
    {
        var addNews = new tblNews();
        addNews.headline = txtHeadlineForm1.Text;
        addNews.headline2 = txtHeadlineForm2.Text;
        addNews.ingress = txtIngressForm.Text;
        addNews.news = txtNewsForm.Text;
        addNews.date = DateTime.Now.ToString();
        db.tblNews.Add(addNews);
        db.SaveChanges();
    }

    lblResult.Text = "Nyhet sparad!";
}

I personally like the first approach of saving if things are valid, because the second approach requires the first thing done is checking for the page not being valid and bailing out (read: return). To each their own.

OTHER TIPS

You need to either include the ScriptManager to do this validation client side:

<asp:ScriptManager />

or you need to check specifically if the page is valid after postback:

if(Page.IsValid)
{
    using (dbnewsEntities db = new dbnewsEntities())
    {
        var addNews = new tblNews();
        addNews.headline = txtHeadlineForm1.Text;
        addNews.headline2 = txtHeadlineForm2.Text;
        addNews.ingress = txtIngressForm.Text;
        addNews.news = txtNewsForm.Text;
        addNews.date = DateTime.Now.ToString();
        db.tblNews.Add(addNews);
        db.SaveChanges();
    }
}

You should be use

ValidationGroup="newsForm" on RequiredFieldValidator field and Button control not use in Textbox control.

May be your problem is solve and RequiredFieldValidator working properly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top