How to Create a User Registration Form in C#.NET using SQL Server Database and Visual Studio 2022?
bool isAnyEmpty = false;
foreach(Control control in this.Controls)
{
if(control is TextBox)
{
if (string.IsNullOrEmpty(control.Text))
{
isAnyEmpty = true;
break;
}
}
else if(control is DateTimePicker)
{
if (((DateTimePicker)control).Value == null)
{
isAnyEmpty = true;
break;
}
}
else if(control is ComboBox)
{
if (((ComboBox)control).SelectedIndex == -1)
{
isAnyEmpty = true;
break;
}
}
}
if (isAnyEmpty)
{
MessageBox.Show("one or more fields are empty, please fill it before submitting");
}
else
{
SqlConnection con = new SqlConnection("Data Source=YOUR-COMPUTER-NAME\\SQLEXPRESS;Initial Catalog=registrationform;Integrated Security=True;TrustServerCertificate=True");
con.Open();
string insertQuery = "INSERT INTO register VALUES (@fname, @lname, @dob, @gender, @email, @username, @password, @phone, @country)";
SqlCommand cmd = new SqlCommand(insertQuery, con);
cmd.Parameters.AddWithValue("@fname", txtFname.Text);
cmd.Parameters.AddWithValue("@lname", txtLname.Text);
cmd.Parameters.AddWithValue("@dob", dtpDOB.Value);
cmd.Parameters.AddWithValue("@gender", cmbGender.SelectedItem.ToString());
cmd.Parameters.AddWithValue("@email", txtEmail.Text);
cmd.Parameters.AddWithValue("@username", txtUser.Text);
cmd.Parameters.AddWithValue("@password", txtPass.Text);
cmd.Parameters.AddWithValue("@phone", txtPhone.Text);
cmd.Parameters.AddWithValue("@country", txtCtry.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("register successfully", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}