Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8369357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:37:24+00:00 2026-06-09T13:37:24+00:00

Well, I have a simple program that is writing to a Database. I am

  • 0

Well, I have a simple program that is writing to a Database. I am trying to add validation to the textbox like this,

private void textBox1_TextChanged(object sender, EventArgs e)
{
    try
    {
         if (textBox1.Text.Length < -1)
         {
             MessageBox.Show("Don't Leave this field blank!");
         }
    }
    catch
    {
         //todo
    }
} 

and My save to database code,

private void button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("Server = DAFFODILS-PC\\SQLEXPRESS;Database=Library;Trusted_Connection=True;");
    SqlCommand sql1 = new SqlCommand("INSERT into Book VALUES('" + textBox1.Text + "' , '" + textBox2.Text + "','" + dateTimePicker1.Value.ToShortDateString() + "')", con);
     con.Open();
     sql1.ExecuteNonQuery();
     con.Close();
     this.bookTableAdapter.Fill(this.booksDataSet.Book);
     MessageBox.Show("Data Added!");
     this.Close();    
}

But still it is adding blank data to the database and strange thing is in the database I have not allowed null but still data is getting added. Any clues where I am wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:37:25+00:00Added an answer on June 9, 2026 at 1:37 pm

    I don’t see you stopping the database to add empty content. You are validating on the textbox_textchanged event which will only validate the text when someone enters the data. You need to put the validation on button1’s click event like this:

        private void button1_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(textBox1.Text.Trim()))
            {
                MessageBox.Show("Null String !!!!!!!!");
                return;
            }
            SqlConnection con = new SqlConnection("Server = DAFFODILS-PC\\SQLEXPRESS;Database=Library;Trusted_Connection=True;");
            SqlCommand sql1 = new SqlCommand("INSERT into Book VALUES('" + textBox1.Text + "' , '" + textBox2.Text + "','" + dateTimePicker1.Value.ToShortDateString() + "')", con);
            con.Open();
            sql1.ExecuteNonQuery();
            con.Close();
            this.bookTableAdapter.Fill(this.booksDataSet.Book);
            MessageBox.Show("Data Added!");
            this.Close();
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, i have written a simple python program that parses HTML with HTMLParser. Here
all. I have built a simple jQuery/PHP chat program that works rather well. However,
I've written a simple application that works with database. My program have a table
well i have a configuration like this in the components part of my config
Well i have a div tag that i show as a modal dialog this
I am writing a simple console application in Objective-C but I have heard that
I got this program that worked well 2 weeks ago, I didn't change any
i'm trying to create a simple Opencv program in C that creates a file
I am writing a program in C# that needs to support undo/redo. For this
So I have this Java program that I use to munch through several terabytes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.