I went through many threads here, I also found a few threads related to what I need, but none worked. So please if anyone can check if there's any mistake in my code or tell me what exactly I should do...
Actually I am able to view the data which is present in the database in a datagrid, now what I exactly want is to delete the selected fields from datagrid as well as database, by pressing the delete button.
Here's my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace Marketing_Resource
{
public partial class frmMainForm : Form
{
public frmMainForm()
{
InitializeComponent();
}
private void label3_Click(object sender, EventArgs e)
{
}
private void btnAddData_Click(object sender, EventArgs e)
{
frmAddExcel tempfrm = new frmAddExcel();
tempfrm.ShowDialog();
}
private void btnShowAllData_Click(object sender, EventArgs e)
{
string executableName = Application.ExecutablePath;
FileInfo executableFileInfo = new FileInfo(executableName);
string parentName = executableFileInfo.Directory.Parent.FullName;
executableName = parentName;
executableFileInfo = new FileInfo(executableName);
parentName = executableFileInfo.Directory.Parent.FullName;
SqlConnection DBCon = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=" + parentName + "\\Marketing Resource\\Students.mdf;Integrated Security=True;User Instance=True");
SqlDataAdapter da;
try
{
string qry = "select * from StudentDetails";
int i = 0;
da = new SqlDataAdapter(qry, DBCon);
DataSet ds = new DataSet();
da.Fill(ds, "stud");
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
catch (Exception ex)
{
lblCheck.Text = ex.Message;
}
finally
{ DBCon.Close(); }
}
private void btnSearch_Click(object sender, EventArgs e)
{
}
private void frmMainForm_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
dataGridView1_Object.Items.Remove(dataGridView1_Object.SelectedIndex);
}
}
}