Question



1. C# Code (40) You have three textboxes. You have a table: Faculty (FID, Name, Office) Assuming you have Access DatasSourcel
0 0
Add a comment Improve this question Transcribed image text
Answer #1

This code will Insert,Delete,Update the data into faculty table.

using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
  
namespace InsertUpdateDeleteDemo
{
public partial class frmMain : Form
{
SqlConnection con= new SqlConnection("Data Source=.;Initial Catalog=Sample;Integrated Security=true;");
SqlCommand cmd;
SqlDataAdapter adapt;
//ID variable used in Updating and Deleting Record
int ID = 0;
public frmMain()
{
InitializeComponent();
DisplayData();
}
//Insert Data
private void btn_Insert_Click(object sender, EventArgs e)
{
if (txt_Fid.Text != "" && txt_Name.Text != ""&&txt_Office.Text != "")
{
cmd = new SqlCommand("insert into Faculty(Fid,Name,Office) values(@Fid,@name,@Office)", con);
con.Open();
cmd.Parameters.AddWithValue("@FID", txt_Fid.Text);
cmd.Parameters.AddWithValue("@Name", txt_Name.Text);
cmd.Parameters.AddWithValue("@Office", txt_Office.Text);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Inserted Successfully");
DisplayData();
ClearData();
}
else
{
MessageBox.Show("Please Provide Details!");
}
}
//Display Data in DataGridView
private void DisplayData()
{
con.Open();
DataTable dt=new DataTable();
adapt=new SqlDataAdapter("select * from Faculty",con);
adapt.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
//Clear Data
private void ClearData()
{
txt_Name.Text = "";
txt_State.Text = "";
ID = 0;
}
//dataGridView1 RowHeaderMouseClick Event
private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
ID = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
txt_Fid.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
txt_Name.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
txt_Office.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
}
//Update Record
private void btn_Update_Click(object sender, EventArgs e)
{
if (txt_Fid.Text != "" && txt_Name.Text != "" && txt_Name.Office != "")
{
cmd = new SqlCommand("update Faculty set Fid=@FID, Name=@Name,Office=@Office where ID=@id", con);
con.Open();
cmd.Parameters.AddWithValue("@id", ID);
cmd.Parameters.AddWithValue("@Fid", txt_Fid.Text);
cmd.Parameters.AddWithValue("@Name", txt_Name.Text);
cmd.Parameters.AddWithValue("@Office", txt_Office.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Record Updated Successfully");
con.Close();
DisplayData();
ClearData();
}
else
{
MessageBox.Show("Please Select Record to Update");
}
}
//Delete Record
private void btn_Delete_Click(object sender, EventArgs e)
{
if(ID!=0)
{
cmd = new SqlCommand("delete Faculty where ID=@id",con);
con.Open();
cmd.Parameters.AddWithValue("@id",ID);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Deleted Successfully!");
DisplayData();
ClearData();
}
else
{
MessageBox.Show("Please Select Record to Delete");
}
}
}
}

Add a comment
Know the answer?
Add Answer to:
1. C# Code (40) You have three textboxes. You have a table: Faculty (FID, Name, Office) Assuming you have Access DatasSourcel objeet, which linked to the Table Faculty. (1) Write the code for a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT