how to validate user input? (c# windows forms application)

Brian 1 Reputation point
2022-11-04T10:54:45.813+00:00

I'm trying to validate user input with the database and empty user input to the database. For example, if the user inserts a username that the database already has the data. In that case, it will prompt the user to enter another Username and if the text box is blank, the user can't enter empty data into the database.

Below is the code I've been trying:

private void btnEnter_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
SqlCommand command = new SqlCommand
{
Connection = conn
};

        if (txtUser.Text != "")  
        {  
            conn.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True";  
            conn.Open();  

            this.Hide();  
            Home home = new Home();  
            home.Show();  

            command.CommandText = "INSERT INTO tblUser(Username) values ('" + txtUser.Text + "')";  
            command.ExecuteNonQuery();  
            conn.Close();  
        }  
        else  
        {  
            lblValidation.Visible = true;  
            txtUser.Focus();  

        }  

        string username = txtUser.Text;  
        conn.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True";  
        command.CommandType = CommandType.Text;  
        command.Parameters.Add("@username", SqlDbType.VarChar).Value = username;  
        command.CommandText = "SELECT * FROM tblUser WHERE Username = @username";  
        command.Connection = conn;  
        SqlDataReader reader = null;  
        conn.Open();  
        reader = command.ExecuteReader();  
        if (reader.Read() || (txtUser.Text != ""))  
        {  
            MessageBox.Show("The username is already available, type a new one");  
            reader.Close();  
        }  
        conn.Close();  
    }  
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,870 questions
{count} votes

1 answer

Sort by: Most helpful
  1. satya karki 986 Reputation points
    2022-11-06T07:32:21.427+00:00

    Hi, before inserting it into the database you need to check whether the username is already available in the database or not. You can try the below code.

     if (txtUser.Text != "")  
             {  
                 conn.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True";  
                 conn.Open();		   
    			   
    		 string username = txtUser.Text;  
             command.CommandType = CommandType.Text;  
             command.Parameters.Add("@username", SqlDbType.VarChar).Value = username;  
             command.CommandText = "SELECT * FROM tblUser WHERE Username = @username";  
             command.Connection = conn;  
             SqlDataReader reader = null;  
             conn.Open();  
             reader = command.ExecuteReader();  
             if (reader.HasRows)  
             {  
                 MessageBox.Show("The username is already available, type a new one");  
                 reader.Close();  
             }  
    		 else{  
    		     
                 command.CommandText = "INSERT INTO tblUser(Username) values ('" + txtUser.Text + "')";  
                 command.ExecuteNonQuery();  
                 conn.Close();  
    			 this.Hide();  
                 Home home = new Home();  
                 home.Show();  
    		 }		   
                 
             }  
             else  
             {  
                 lblValidation.Visible = true;  
                 txtUser.Focus();  
             }  
               
             conn.Close();  
         }  
    
    1 person found this answer helpful.
    0 comments No comments