|
It shows record updated successfully but not updating in database
private void button13_Click(object sender, EventArgs e)
{
if (textBox6.Text != "" && textBox7.Text != "" && textBox8.Text != "")
{
string connectionString;
MySqlConnection cnn;
connectionString = @"Data Source=localhost;Initial Catalog=testDB;User ID=root;Password=mysql";
cnn = new MySqlConnection(connectionString);
//string id = Convert.ToInt32(textBox6.Text);
string id = textBox6.Text;
string name = textBox7.Text;
string salary = textBox8.Text;
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
string query = "update employee set employee_id='" + this.textBox6.Text + "',employee_name='" + this.textBox7.Text + "',employee_salary='" + this.textBox8.Text + "' where employee_id='" + this.textBox6.Text + "';";
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Parameters.AddWithValue("@employee_id", id);
cmd.Parameters.AddWithValue("@employee_name", name);
cmd.Parameters.AddWithValue("@employee_salary", salary);
cmd.Connection = cnn;
cnn.Open();
cmd.ExecuteNonQuery();
DialogResult dr = MessageBox.Show("Are you sure to update row?", "Confirmation", MessageBoxButtons.YesNo);
if (dr == DialogResult.Yes)
{
MessageBox.Show("Record Updated Successfully");
cnn.Close();
DisplayData();
ClearData();
}
else if (dr == DialogResult.No)
{
MessageBox.Show("Dude, We keep this record as same");
}
}
}
else
{
MessageBox.Show("Please Select Record to Update");
}
}
modified 20-Mar-22 11:58am.
|
|
|
|
|
|
Yes sir. Output came. Thanks to all
|
|
|
|
|
Dude.
No.
Please burn it.
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
|
I thought we discussed this ... your reply less than 5 hours ago was "Ok sir"
Is there any point in us talking to you at all?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
if (textBox6.Text != "" && textBox7.Text != "" && textBox8.Text != "") No.
Nuke it from orbit. That's the only way to be sure.
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
I don't think he really wants to learn - just get his homework done for him ... where he can't copy'n'paste a solution.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Where would one start? At textBox7 , or the missing usings?
I'd burn the code and never look back.
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
|
So, it's obvious you're just copying and pasting code from the web and hoping it works. You don't have any idea why there are parameter objects or how they are used in the query.
string query = "update employee set employee_id='" + this.textBox6.Text + "',employee_name='" + this.textBox7.Text + "',employee_salary='" + this.textBox8.Text + "' where employee_id='" + this.textBox6.Text + "';";
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Parameters.AddWithValue("@employee_id", id);
cmd.Parameters.AddWithValue("@employee_name", name);
cmd.Parameters.AddWithValue("@employee_salary", salary);
You're using the TextBox values directly in your query. NEVER DO THIS! User input should be treated like it's the spawn of Satan. Using it directly in your query will lead to SQL Injection Attacks and you risk destroying your database doing that.
Next, you NEVER change or update the value of an ID field in a table. Doing so will destroy your data integrity since records in one table will no longer relate to data in another table.
Your query should look like this (and don't even think of copying and pasting this code!) Try to figure out what the code is doing.
string query = "UPDATE employee SET employee_name=@empName, employee_salary=@empSalary WHERE employee_id=@empId";
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Parameters.AddWithValue("@empId", id);
cmd.Parameters.AddWithValue("@empName", name);
cmd.Parameters.AddWithValue("@empSalary", salary);
I'm ignoring that you're storing salary values as text.
|
|
|
|
|
Noted with thanks. its works
|
|
|
|
|
This could hardly be more wrong
cmd.ExecuteNonQuery();
DialogResult dr = MessageBox.Show("Are you sure to update row?", "Confirmation", MessageBoxButtons.YesNo);
if (dr == DialogResult.Yes)
{
MessageBox.Show("Record Updated Successfully");
You call ExecuteNonQuery but ignore the return value, so if it failsyou will never know. You then ask the user whether to update the row, which (you think) you already did. And if the user answers "Yes", you then post a message to say the update succeeded.
|
|
|
|
|
|
|
I seriously doubt it works in all situations. Just because you get a dialog box that says the record is updated, doesn't mean your logic to that point is correct. Given your history, I think it's still very, very wrong.
|
|
|
|
|
if (textBox6.Text!= "" && textBox7.Text != "" && textBox8.Text != "")
{
string connectionString;
MySqlConnection cnn;
connectionString = @"Data Source=localhost;Initial Catalog=testDB;User ID=root;Password=mysql";
cnn = new MySqlConnection(connectionString);
//cnn.Open();
//cnn.Close();
string id = textBox6.Text;
string name = textBox7.Text;
string salary = textBox8.Text;
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
string query = "delete employee where(@employee_id, @employee_name, @employee_salary)";
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Parameters.AddWithValue("@employee_id", id);
cmd.Parameters.AddWithValue("@employee_name", name);
cmd.Parameters.AddWithValue("@employee_salary", salary);
cmd.Connection = cnn;
cnn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Record Deleted Successfully");
cnn.Close();
}
}
else
{
MessageBox.Show("Please Select Record to Delete");
}
|
|
|
|
|
That's not a valid SQL DELETE statement. You're trying telling it to delete the fields in a row and that's not how it works.
The correct format is, assuming your table name is "employee" and the identity column is "EmployeeID":
DELETE employee WHERE EmployeeId = @employee_id
|
|
|
|
|
if change that it tells please select the record...
if (ID != 0)
//if (textBox6.Text!= "" && textBox7.Text != "" && textBox8.Text != "")
{
string connectionString;
MySqlConnection cnn;
connectionString = @"Data Source=localhost;Initial Catalog=testDB;User ID=root;Password=mysql";
cnn = new MySqlConnection(connectionString);
string id = textBox6.Text;
string name = textBox7.Text;
string salary = textBox8.Text;
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
string query = "DELETE employee WHERE EmployeeId = @employee_id";
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Parameters.AddWithValue("@employee_id", id);
cmd.Parameters.AddWithValue("@employee_name", name);
cmd.Parameters.AddWithValue("@employee_salary", salary);
cmd.Connection = cnn;
cnn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Record Deleted Successfully");
cnn.Close();
DisplayData();
ClearData();
}
}
else
{
MessageBox.Show("Please Select Record to Delete");
}
|
|
|
|
|
You just copied and paste code without even trying to understand it.
I told you, I AM ASSUMING TABLE AND COLUMN NAMES!! You have to change those to match what is in your database!
Also, you have to remove the two line that setup Parameters that are not used, "@employee_name" and "@employee_salary".
|
|
|
|
|
DELETE FROM employee WHERE employee_id=@employee_id AND employee_name=@employee_name AND employee_salary-@employee_salary
this code is working but it shows record deleted successfully but in database its not deleted,, what i do?
|
|
|
|
|
Use the debugger.
Put a breakpoint on the ExecuteNonQuery line:
cmd.ExecuteNonQuery(); and when it hits, look closely at exactly what is in the three variables id , name , and salary .
Then go to your MySql management software and run a SELECT to seem how many records match that criteria.
If it's zero, then no rows will be deleted.
Back to Visual studio, and use step over to execute the ExecuteNonQuery line.
Back to the DB management, and run the query again.
How many rows this time?
If it hasn't changed, then go back to your code and look at it really carefully ... does "-" have the same effect as "="?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
sir, already i put code cmd.executenonquery sir. see my previous coding. but still record not deleting from database
|
|
|
|
|
Do you read anything people tell you, or are you just expecting to be told exactly what to type?
We are trying to teach you how to solve these problems for yourself, by thinking about what you are doing, and looking at what happens - then thinking again about why that wasn't what you expected.
So go back to what I said last time, and read it again. Where in there did I tell you to "put code cmd.executenonquery"? What did I actually tell you to do?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Sir, i have heard and tried what you said, the below coding is here; if i run coding it shows record deleted successfully but still the record not deleting from database
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Windows.Forms;
using System.Data;
using System.Drawing;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;
private void DisplayData()
{
string connectionString;
MySqlConnection cnn;
connectionString = @"Data Source=localhost;Initial Catalog=testDB;User ID=root;Password=mysql";
cnn = new MySqlConnection(connectionString);
DataTable dt = new DataTable();
adapt = new MySqlDataAdapter("select * from employee", cnn);
adapt.Fill(dt);
dataGridView1.DataSource = dt;
cnn.Open();
cnn.Close();
}
private void ClearData()
{
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
ID = 0;
}
private void button9_Click(object sender, EventArgs e)
{
if (textBox6.Text!= "" && textBox7.Text != "" && textBox8.Text != "")
{
string connectionString;
MySqlConnection cnn;
connectionString = @"Data Source=localhost;Initial Catalog=testDB;User ID=root;Password=mysql";
cnn = new MySqlConnection(connectionString);
string id = textBox6.Text;
string name = textBox7.Text;
string salary = textBox8.Text;
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
string query = "DELETE FROM employee WHERE employee_id=@employee_id AND employee_name=@employee_name AND employee_salary-@employee_salary";
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Parameters.AddWithValue("@employee_id", id);
cmd.Parameters.AddWithValue("@employee_name", name);
cmd.Parameters.AddWithValue("@employee_salary", salary);
cmd.Connection = cnn;
cnn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Record Deleted Successfully");
cnn.Close();
DisplayData();
ClearData();
}
}
else
{
MessageBox.Show("Please Select Record to Delete");
}
}
|
|
|
|