•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 402,813 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,053 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 331 | Replies: 3
![]() |
•
•
Join Date: May 2008
Posts: 31
Reputation:
Rep Power: 1
Solved Threads: 0
In relation to my previous post. I was able to delete the rows in the datagridview (thanks to ema005), but now I am having problems with the updating of it in the database. It's deleting in my datagridview alright. But not in my database. My codes are already going on spaghetti just trying to figure it out. Can you help me with this? Here's my code:
This is from my separate class I made:
And this is my code from my Form1
This is my delete button from this form:
This is from my separate class I made:
class dbConn
{
public SqlConnection myConn = new SqlConnection();
public SqlDataAdapter da;
public SqlDataAdapter da2;
public SqlDataAdapter da3;
SqlCommand Command;
SqlCommand aCommand;
SqlCommand rdrCommand;
SqlDataReader rdr;
SqlCommandBuilder cmdBld;
public DataTable dt = new DataTable();
public DataTable dt2 = new DataTable();
public DataTable dt3 = new DataTable();
public DataTable dTable = new DataTable();
public DataSet ds = new DataSet();
public DataSet ds2 = new DataSet();
public DataSet ds3 = new DataSet();
int check;
int iRowIndex = 0;
List<string> strArray = new List<string>();
public void OpenConnect()
{
myConn = new SqlConnection(@"Data Source =.\sqlexpress; integrated security = true; Initial Catalog = mmdaserver;");
}
public void DisplayInfo()
{
OpenConnect();
Command = new SqlCommand("DisplayRecords", myConn);
Command.CommandType = CommandType.StoredProcedure;
aCommand = new SqlCommand("AlarmList", myConn);
aCommand.CommandType = CommandType.StoredProcedure;
da = new SqlDataAdapter(Command);
da.Fill(ds, "Records");
da = new SqlDataAdapter(aCommand);
da.Fill(ds2, "Records");
da2 = new SqlDataAdapter("SELECT * FROM DriverInfo", myConn);
da3 = new SqlDataAdapter("SELECT * FROM Violations", myConn);
da2.Fill(dt);
da2.Fill(ds, "DriverInfo");
da3.Fill(dt);
da3.Fill(ds, "Violations");
myConn.Close();
}
}And this is my code from my Form1
dbConn dbc = new dbConn();
BindingSource bSrc = new BindingSource();
Login thisFirst = new Login();
public Form1()
{
InitializeComponent();
dbc.DisplayInfo();
dataGridRecords.AllowUserToDeleteRows = true;
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
groupBox1.Enabled = false;
groupBox2.Enabled = false;
groupBox3.Enabled = false;
dataGridRecords.DataSource = null;
dataGridAlarm.DataSource = null;
dataGridViolations.DataSource = null;
dataGridDriverInf.DataSource = null;
thisFirst.ShowDialog();
groupBox1.Enabled = true;
groupBox2.Enabled = true;
groupBox3.Enabled = true;
RefreshGrid();
}
public void RefreshGrid()
{
dbc.ds.Clear();
dbc.ds2.Clear();
dbc.da.Update(dbc.dTable);
dbc.DisplayInfo();
dataGridRecords.DataSource = dbc.ds.Tables["Records"];
dataGridAlarm.DataSource = dbc.ds2.Tables["Records"];
dataGridViolations.DataSource = dbc.ds.Tables["Violations"];
dataGridDriverInf.DataSource = dbc.ds.Tables["DriverInfo"];
}This is my delete button from this form:
private void btnDelete_Click(object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show("Are you sure you want to delete this row?", "Confirm deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
dbc.dTable = new DataTable("Records");
foreach (DataGridViewRow row in dataGridRecords.SelectedRows)
{
if (row.Index != dataGridRecords.Rows.Count - 1)
{
dbc.OpenConnect();
dbc.myConn.Open();
dbc.dTable = dbc.ds.Tables[0];
dbc.dTable.Rows[row.Index].Delete();
dbc.dTable.GetChanges();
dbc.dTable.AcceptChanges();
bSrc.DataSource = dbc.dTable;
dataGridRecords.DataSource = bSrc;
dbc.da.Update(dbc.dTable);
}
}
dbc.ds.GetChanges();
if (dbc.ds != null)
{
dbc.ds.AcceptChanges();
}
dbc.myConn.Close();
}
} Last edited by harcaype : Jul 15th, 2008 at 8:20 am.
•
•
Join Date: Jul 2008
Posts: 89
Reputation:
Rep Power: 1
Solved Threads: 9
Hi,
Your problem is that you have deleted rows from the DataTable(this act like a cache but its not related to the fisical DB wich means that whatever you do in the DT doesn't reflecs in the DB) and not in the actual data base what you need to do is run delete domands(SQL) against the DB .
Regards,
Camilo
Your problem is that you have deleted rows from the DataTable(this act like a cache but its not related to the fisical DB wich means that whatever you do in the DT doesn't reflecs in the DB) and not in the actual data base what you need to do is run delete domands(SQL) against the DB .
Regards,
Camilo
Last edited by camilojvarona : Jul 15th, 2008 at 9:38 pm.
•
•
Join Date: Jul 2008
Posts: 89
Reputation:
Rep Power: 1
Solved Threads: 9
Hi,
I've found this link where the compiler create the SQL delete comand for you.
http://searchwindevelopment.techtarg...866086,00.html
note: You just need to change everywhere where has OleDb for Sql
Regards,
Camilo
I've found this link where the compiler create the SQL delete comand for you.
http://searchwindevelopment.techtarg...866086,00.html
note: You just need to change everywhere where has OleDb for Sql
Regards,
Camilo
![]() |
•
•
•
•
•
•
•
•
DaniWeb C# Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Updating Combo Box (Visual Basic 4 / 5 / 6)
Other Threads in the C# Forum
- Previous Thread: executing MouseDown then executing KeyUp (Object sender cant keep track the latest ob
- Next Thread: Save shape(polygon, ellipse) into file


Linear Mode