asp prct7

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

//Design window of Form1.

cs

//Form1.cs source code


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Data.SqlClient;

namespace prct7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection conn = new SqlConnection(@"Data Source=DELL-BM\SQLEXPRESS;Initial
Catalog=college;Integrated Security=True;TrustServerCertificate=True");

private void textBox1_TextChanged(object sender, EventArgs e)


{

private void button2_Click(object sender, EventArgs e)


{
textBox1.Clear();
textBox2.Clear();
textBox1.Focus();
}

private void button1_Click(object sender, EventArgs e)


{
string username, user_password;
username = textBox1.Text;
user_password = textBox2.Text;
try
{
string query = " SELECT * from college1 where username= '" + textBox1.Text
+ "'AND password = '" + textBox2.Text + "'";
SqlDataAdapter sda = new SqlDataAdapter(query, conn);
DataTable dtable = new DataTable();
sda.Fill(dtable);
if (dtable.Rows.Count > 0)
{
username = textBox1.Text;
user_password = textBox2.Text;
MessageBox.Show("You are Logged in Successfully");
MenuForms form2 = new MenuForms();
form2.Show();
}
else
{
MessageBox.Show("Invalid login details", "error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
textBox1.Clear();
textBox2.Clear();
textBox1.Focus();
}
}
catch (SqlException ex)
{
MessageBox.Show("Database error: " + ex.Message);
}
catch (Exception ex)
{
MessageBox.Show("An error occurred: " + ex.Message);
}
finally
{

}
}

private void button3_Click(object sender, EventArgs e)


{
DialogResult res;
res = MessageBox.Show("Do you want to exit?", "exit", MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (res == DialogResult.Yes)
{
Application.Exit();
}
else
{
this.Show();
}
}
}
}
//MenuForms.cs(Design)

//MenuForms.cs sourcecode

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Data.SqlClient;

namespace prct7
{
public partial class MenuForms : Form
{
public MenuForms()
{
InitializeComponent();
}
SqlConnection conn = new SqlConnection(@"Data Source=DELL-BM\SQLEXPRESS;Initial
Catalog=student;Integrated Security=True;TrustServerCertificate=True");

private void label4_Click(object sender, EventArgs e)


{

private void textBox1_TextChanged(object sender, EventArgs e)


{

private void MenuForms_Load(object sender, EventArgs e)


{

private void button1_Click(object sender, EventArgs e)


{
// Get data from textboxes
string name = textBox1.Text;
string rollno = textBox2.Text;
string mobilenumber = textBox3.Text;
string emailid = textBox4.Text;
try
{
// Open the database connection
conn.Open();

// Create a parameterized SQL query


string query = "INSERT INTO student1(name, rollno, [mobileno], [email])
VALUES (@name, @rollno, @mobilenumber, @emailid)";
SqlCommand cmd = new SqlCommand(query, conn);

// Add values to the parameters


cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@rollno", rollno);
cmd.Parameters.AddWithValue("@mobilenumber", mobilenumber);
cmd.Parameters.AddWithValue("@emailid", emailid);

// Execute the query and check how many rows were affected
int rowsAffected = cmd.ExecuteNonQuery();

// Display a message if data was inserted successfully


if (rowsAffected > 0)
{
MessageBox.Show("Data Inserted Successfully");
}
else
{
MessageBox.Show("Data insertion failed.");
}
}
catch (SqlException ex)
{
MessageBox.Show("Database error: " + ex.Message);
}
catch (Exception ex)
{
MessageBox.Show("An error occurred: " + ex.Message);
}
finally
{
// Always close the database connection in the 'finally' block
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
}

private void button2_Click(object sender, EventArgs e)


{
// Clear the textboxes and set focus back to the first one
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox1.Focus();
}

private void button3_Click(object sender, EventArgs e)


{
// Show the previous form and hide this one
Form1 form1 = new Form1();
form1.Show();
this.Hide();
}
}
}
Output window

If we click on login bu on
Now click on ok

Fill the informa on in the above form

If we click on submit bu on

Click ok
Now our informa on is saved in student1 table in Student Database

If we click on clear bu on

If we click on exit bu on then we will be on Form1


Now click on exit bu on->Yes and we will be out of Form1

We can see our data is stored in the Database

You might also like