//Design window of Form1.
cs
//Form1.cs source code
us ng System;
us ng System.Collect ons.Gener
c; us ng System.ComponentModel;
us ng System.Data;
us ng System.Draw
ng; us ng System.L
nq;
us ng System.Text;
us ng System.Thread
ng.Tasks; us ng System.W
ndows.Forms;
us ng M crosoft.Data.SqlCl ent;
namespace prct7
{
publ c part al class Form1 : Form
{
publ c Form1()
{
In t al zeComponent();
}
SqlConnect on conn = new SqlConnect on(@"Data Source=DELL-BM\
SQLEXPRESS;In t al Catalog=college;Integrated Secur ty=True;TrustServerCert f
cate=True");
pr vate vo d textBox1_TextChanged(object sender, EventArgs e)
{
pr vate vo d button2_Cl ck(object sender, EventArgs e)
{
textBox1.Clear(
);
textBox2.Clear(
);
textBox1.Focus
();
}
pr vate vo d button1_Cl ck(object sender, EventArgs e)
{
str ng username,
user_password; username =
textBox1.Text; user_password
= textBox2.Text; try
{
str ng query = " SELECT * from college1 where username= '" +
textBox1.Text
+ "'AND password = '" + textBox2.Text + "'";
SqlDataAdapter sda = new SqlDataAdapter(query,
conn); DataTable dtable = new DataTable();
sda.F ll(dtable);
f (dtable.Rows.Count > 0)
{
username = textBox1.Text;
user_password =
textBox2.Text;
MessageBox.Show("You are Logged n
Successfully"); MenuForms form2 = new
MenuForms();
form2.Show();
}
else
{
MessageBox.Show("Inval d log n deta ls", "error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Clear(
);
textBox2.Clear(
);
textBox1.Focus
();
}
}
catch (SqlExcept on ex)
{
MessageBox.Show("Database error: " + ex.Message);
}
catch (Except on ex)
{
MessageBox.Show("An error occurred: " + ex.Message);
}
f nally
{
}
}
pr vate vo d button3_Cl ck(object sender, EventArgs e)
{
D alogResult res;
res = MessageBox.Show("Do you want to ex t?", "ex t",
MessageBoxButtons.YesNo, MessageBoxIcon.Quest on);
f (res == D alogResult.Yes)
{
Appl cat on.Ex t();
}
els
e
{ th s.Show();
}
}
}
}
//MenuForms.cs(Des gn)
//MenuForms.cs sourcecode
us ng System;
us ng System.Collect ons.Gener
c; us ng System.ComponentModel;
us ng System.Data;
us ng System.Draw
ng; us ng System.L
nq;
us ng System.Text;
us ng System.Thread
ng.Tasks; us ng System.W
ndows.Forms;
us ng M crosoft.Data.SqlCl ent;
namespace prct7
{
publ c part al class MenuForms : Form
{
publ c MenuForms()
{
In t al zeComponent();
}
SqlConnect on conn = new SqlConnect on(@"Data Source=DELL-BM\
SQLEXPRESS;In t al Catalog=student;Integrated Secur ty=True;TrustServerCert f
cate=True");
pr vate vo d label4_Cl ck(object sender, EventArgs e)
{
pr vate vo d textBox1_TextChanged(object sender, EventArgs e)
{
pr vate vo d MenuForms_Load(object sender, EventArgs e)
{
pr vate vo d button1_Cl ck(object sender, EventArgs e)
{
// Get data from textboxes
str ng name =
textBox1.Text;
str ng rollno = textBox2.Text;
str ng mob lenumber =
textBox3.Text; str ng ema l d =
textBox4.Text;
try
{
// Open the database connect on
conn.Open();
// Create a parameter zed SQL query
str ng query = "INSERT INTO student1(name, rollno, [mob leno], [ema
l]) VALUES (@name, @rollno, @mob lenumber, @ema l d)";
SqlCommand cmd = new SqlCommand(query, conn);
// Add values to the parameters
cmd.Parameters.AddW thValue("@name", name);
cmd.Parameters.AddW thValue("@rollno",
rollno);
cmd.Parameters.AddW thValue("@mob lenumber", mob lenumber);
cmd.Parameters.AddW thValue("@ema l d", ema l d);
// Execute the query and check how many rows were
affected nt rowsAffected = cmd.ExecuteNonQuery();
// D splay a message f data was
nserted successfully f
(rowsAffected > 0)
{
MessageBox.Show("Data Inserted Successfully");
}
else
{
MessageBox.Show("Data nsert on fa led.");
}
}
catch (SqlExcept on ex)
{
MessageBox.Show("Database error: " + ex.Message);
}
catch (Except on ex)
{
MessageBox.Show("An error occurred: " + ex.Message);
}
f nally
{
// Always close the database connect on n the 'f
nally' block f (conn.State == Connect onState.Open)
{
conn.Close();
}
}
}
pr vate vo d button2_Cl ck(object sender, EventArgs e)
{
// Clear the textboxes and set focus back to the f
rst one textBox1.Clear();
textBox2.Clear(
);
textBox3.Clear(
);
textBox4.Clear(
);
textBox1.Focus
();
}
pr vate vo d button3_Cl ck(object sender, EventArgs e)
{
// Show the prev ous form and h de th s one
Form1 form1 = new Form1();
form1.Show();
th s.H de();
}
}
}
Output window
If we click on login button
Now click on ok
Fill the information in the above form
If we click on submit button
Click ok
Now our information is saved in student1 table in Student Database
If we click on clear button
If we click on exit button then we will be on Form1
Now click on exit button->Yes and we will be out of Form1
We can see our data is stored in the Database