OdbcConnection conn = new OdbcConnection();
conn.ConnectionString =
"Dsn=mdc;" +
"Uid=root;" +
"Pwd=;";
OdbcCommand cmd = new OdbcCommand("UPDATE tbl_delivery SET (Supplier, InvoiceNumber, DRNumber, PONumber, ItemQty, ReceivedDate, Address, Contact, ReceivedBy, AssetNumber) (Supplier ='" + this.supplierTextBox.Text + "',InvoiceNumber ='" + this.invoiceNumberTextBox.Text + "',DRNumber ='" + this.dRNumberTextBox.Text + "',PONumber ='" + this.pONumberTextBox.Text + "',ItemQty ='" + this.itemQtyTextBox.Text + "',ReceivedDate ='" + this.receivedDateDateTimePicker.Text + "',Address ='" + this.addressTextBox.Text + "',Contact ='" + this.contactTextBox.Text + "',ReceivedBy ='" + this.receivedByTextBox.Text + "',AssetNumber ='" + this.assetNumberTextBox.Text + "'", conn);
cmd.CommandType = CommandType.Text;
OdbcDataAdapter ds = new OdbcDataAdapter(cmd);
ds.SelectCommand = cmd;
System.Data.DataTable dtable = new System.Data.DataTable();
ds.Fill(dtable);
tbl_deliveryDataGridView.DataSource = dtable;
conn.Open();
cmd.ExecuteNonQuery();
update button won't work, please check if my update statement is correct. i am using c#..............
I think your Query is incorrect.
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString =
"Dsn=mdc;" +
"Uid=root;" +
"Pwd=;";
OdbcCommand cmd = new OdbcCommand("UPDATE tbl_delivery SET Supplier ='" + this.supplierTextBox.Text + "',InvoiceNumber ='" + this.invoiceNumberTextBox.Text + "',DRNumber ='" + this.dRNumberTextBox.Text + "',PONumber ='" + this.pONumberTextBox.Text + "',ItemQty ='" + this.itemQtyTextBox.Text + "',ReceivedDate ='" + this.receivedDateDateTimePicker.Text + "',Address ='" + this.addressTextBox.Text + "',Contact ='" + this.contactTextBox.Text + "',ReceivedBy ='" + this.receivedByTextBox.Text + "',AssetNumber ='" + this.assetNumberTextBox.Text + "'", conn);
Then where is your Where clause? just add it on the query
Then
cmd.ExecuteNonQuery();
ds= newodbcDataAdapter(cmd);
ds.Fill(dtable);
tbl_deliveryDataGridView.ItemsSource = dtable.DefaultView;
You better execute your query first before displaying it to your datagrid for that you'll able to see the updated table.
Related
Iam trying to figure out a way to select a value on a comboBox which would then be used for and mysql command.
Iam new to coding in c# so i got know clue where my mistake should be.. searched some hours now and found nothing.
It is supposed to be used as an filter to search through a huge stack of customers data to for example only show customers living in Berlin, oder working as...
if (checkBoxrecruitingsearch2.Checked)
{
MySqlCommand cmddtschichten = conn.CreateCommand();
cmddtschichten.CommandType = CommandType.Text;
cmddtschichten.CommandText = "SELECT id, Wohnort, Berufsbezeichnung FROM bewerber WHERE '"+ comboBoxrecruitingfilter.Text +"' = '" + textBoxrecruitingsearch.Text + "' and '" + comboBoxrecruitingsearch2.Text + "' = '" + textBoxrecruitingsearch2.Text + "'";
cmddtschichten.ExecuteNonQuery();
DataTable dtschichten = new DataTable();
MySqlDataAdapter cmddaschichten = new MySqlDataAdapter(cmddtschichten);
cmddaschichten.Fill(dtschichten);
dtschichten.Columns["id"].ColumnName = "Bewerber ID";
dtschichten.Columns["Wohnort"].ColumnName = "Wohnort";
dtschichten.Columns["Berufsbezeichnung"].ColumnName = "Berufsbezeichnung";
BindingSource bSourceschichten = new BindingSource();
bSourceschichten.DataSource = dtschichten;
dataGridViewrecruitingsearchresult.DataSource = bSourceschichten;
cmddaschichten.Update(dtschichten);
}
else
{
MySqlCommand cmddtschichten = conn.CreateCommand();
cmddtschichten.CommandType = CommandType.Text;
cmddtschichten.CommandText = "SELECT id, Wohnort, Berufsbezeichnung FROM bewerber WHERE '"+ comboBoxrecruitingfilter.Text +"' = '" + textBoxrecruitingsearch.Text + "'";
cmddtschichten.ExecuteNonQuery();
DataTable dtschichten = new DataTable();
MySqlDataAdapter cmddaschichten = new MySqlDataAdapter(cmddtschichten);
cmddaschichten.Fill(dtschichten);
dtschichten.Columns["id"].ColumnName = "Bewerber ID";
dtschichten.Columns["Wohnort"].ColumnName = "Wohnort";
dtschichten.Columns["Berufsbezeichnung"].ColumnName = "Berufsbezeichnung";
BindingSource bSourceschichten = new BindingSource();
bSourceschichten.DataSource = dtschichten;
dataGridViewrecruitingsearchresult.DataSource = bSourceschichten;
cmddaschichten.Update(dtschichten);
}
the expected result would be to get back all "bewerber" where column (choosen on comboBoxrecruitingfilter) equals textBoxrecuitingsearch
I do get shown a list with all entry's when executing with empty text and combobox, otherwise it shows nothing
SOLUTION
If I remember correctly mysql does have a specific column name delimiter " ` " but in your where you are using " ' ". Very similar but different characters
WHERE ' "+ comboBoxrecruitingfilter.Text +" ' = '" + textBoxrecruitingsearch.Text + " '
should be:
WHERE ` "+ comboBoxrecruitingfilter.Text +" ` = '" + textBoxrecruitingsearch.Text + " '
As a side note, your code is subject to SQL Injection, if your users are not trusted you have a security problem/could be hacked. Should move to parametrized queries instead.
Remove the ' around the field name in the where clause:
cmddtschichten.CommandText = "SELECT id, Wohnort, Berufsbezeichnung FROM bewerber WHERE "+ comboBoxrecruitingfilter.Text +" = '" + textBoxrecruitingsearch.Text + "' and " + comboBoxrecruitingsearch2.Text + " = '" + textBoxrecruitingsearch2.Text + "'";
and
cmddtschichten.CommandText = "SELECT id, Wohnort, Berufsbezeichnung FROM bewerber WHERE "+ comboBoxrecruitingfilter.Text +" = '" + textBoxrecruitingsearch.Text + "'";
If you have space between your field names, you can use one of the following:
cmddtschichten.CommandText = "SELECT id, Wohnort, Berufsbezeichnung FROM bewerber WHERE `"+ comboBoxrecruitingfilter.Text +"` = '" + textBoxrecruitingsearch.Text + "'";
or
cmddtschichten.CommandText = "SELECT id, Wohnort, Berufsbezeichnung FROM bewerber WHERE ["+ comboBoxrecruitingfilter.Text +"] = '" + textBoxrecruitingsearch.Text + "'";
Tried to move data from one form to another and there is a problem with the table. Yes I found such themes with a mistake, and tried to correct himself, but something went wrong.
using (SqlConnection conn = new SqlConnection("Data Source=DESKTOP-R552818\\SQLEXPRESS;Initial Catalog=Fond;Integrated Security=True"))
{
SqlDataAdapter comm = new SqlDataAdapter("INSERT INTO Pacient (Name, id_diagnoz, Surname, Middle_name, Column__Passport, Legal_address_Clinic, Age) " +
"VALUES ('"+ tName.Text + "', (SELECT id_diagnoz FROM Diagnoz WHERE Name_diagnoz = '" + cbName.Text + "' and Stage = '" + cbStage.Text + "'), '" + tSurname.Text + "', '" + tMiddle.Text + "', '" + tPas.Text + "', '" + cbClinic.Text + "', '" + tAge.Text + "')", conn);
conn.Open();
DataSet ds = new DataSet();
//ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
comm.Fill(ds);
Form1 form = new Form1();
form.DataGrid.DataSource = ds.Tables[0]; //?
}
string connectionString = "Data Source=DESKTOP-R552818\\SQLEXPRESS;Initial Catalog=Fond;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlTransaction transaction = connection.BeginTransaction();
SqlCommand command = connection.CreateCommand();
command.Transaction = transaction;
try
{
command.CommandText = "INSERT INTO Pacient (Name, id_diagnoz, Surname, Middle_name, Column__Passport, Legal_address_Clinic, Age) " +
"VALUES ('" + metroTextBox1.Text + "', (SELECT id_diagnoz FROM Diagnoz WHERE Name_diagnoz = '" + metroComboBox1.Text + "' and Stage = '" + metroComboBox2.Text + "'), '" + metroTextBox2.Text + "', '" + metroTextBox3.Text + "', '" + maskedTextBox1.Text + "', '" + metroComboBox3.Text + "', '" + metroTextBox5.Text + "')";
command.ExecuteNonQuery();
transaction.Commit();
MessageBox.Show("Added");
//here is a DataSet
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
transaction.Rollback();
}
}
You are expecting results to be returned from your query, but what you do is just INSERT statement.
For inserting values you should use ExecuteNonQuery method of SqlCommand (see this for reference).
Then, assign another command: SELECT to get the results, then you can fill DataSet with the result and then you can fill DataGridView with it.
Also: you are rpone to SQL injection, use parametrized query to prevent yourself from such threat (see this for reference).
I am new to the C# programming. Facing the problem Incorrect syntax near 'First_Name'.! in the given below code:
private void button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = #"Data Source=HP\SQLEXPRESS100;Database=CD_Gallery;Integrated Security=true";
con.Open();
if (con.State == System.Data.ConnectionState.Open)
{
SqlCommand cmd = new SqlCommand("update Customer_Info First_Name ='" + fname.Text + "'");
//'" + fname.Text.ToString() + "','" + lname.Text.ToString() + "','" + landmark.Text.ToString() + "','" + address.Text.ToString() + "','" + contact.Text.ToString() + "','" + email.Text.ToString() + "','" + dateTimePicker1.Text.ToString() + "','" + deposite.Text.ToString() + "')", con);
cmd.Connection = con;
cmd.CommandType = System.Data.CommandType.Text;
int a = cmd.ExecuteNonQuery();
if (a > 0)
{
MessageBox.Show("You Have Successfully Updated");
Custid.Text = "";
fname.Text = "";
lname.Text = "";
address.Text = "";
contact.Text = "";
email.Text = "";
landmark.Text = "";
deposite.Text = "";
}
}
}
Problem : You forgot to add word SET after your table name in update statement.
Solution1 : Add the word SET after table name in Update query (Don't Recommend this)
"update Customer_Info SET First_Name ='" + fname.Text + "'"
Warning : Your query is open to sql injection attacks.please use parameterised queries to avoid them
Solution 2: Using Parameterised Queries
Replace This:
SqlCommand cmd = new SqlCommand("update Customer_Info SET First_Name
='"+fname.Text+"'");
With This:
SqlCommand cmd = new SqlCommand("update Customer_Info First_Name = #fname");
cmd.Parameters.AddWithValue("#fname" , fname.Text);
Your problem not in C#, in SQL syntax (you miss set keyword)
SqlCommand("update Customer_Info set First_Name ='" + fname.Text + "'");
you are missing SET keyword:
update Customer_Info SET First_Name ='" + fname.Text + "'"
and also provide where clause otherwise it will update all the records in your table.
You are missing set keyword in query you have to place set like this
SqlCommand cmd = new SqlCommand("update Customer_Info set First_Name ='" + fname.Text + "'");
i have a button that suppose to update data into the database.
private void button4_Click(object sender, EventArgs e)
{
//need update code//
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
SqlDataAdapter daCount = new SqlDataAdapter("select iCount from ComDet where cName = #cName", conn);
daCount.SelectCommand.Parameters.Add("#cName", SqlDbType.VarChar).Value = ListU.SelectedValue;
DataTable dtC = new DataTable();
daCount.Fill(dtC);
DataRow firstRow = dtC.Rows[0];
string x = firstRow["iCount"].ToString();
int y = Int32.Parse(x);
int z = y + 1;
//SqlCeCommand cmdC = conn.CreateCommand();
SqlCommand cmdC = conn.CreateCommand();
cmdC.CommandText = "Update ComDet set iCount = '" + z + "', ViewTime = '" + lblTime.Text + "', LastView = '" + txtUser2.Text + "' Where cName = '" + ListU.SelectedValue.ToString() + "'";
conn.Close();
}
but i get this error..
can someone help?
update =
i've changed my code to
cmdC.CommandText = "Update ComDet set iCount = " + z + ", ViewTime = '" + lblTime.Text + "', LastView = '" + txtUser2.Text + "' Where cName = '" + ListU.SelectedValue.ToString() + "'";
but the problem now is that , there's no update.
the iCount in the database is an INT , value is 0.
There is also no update for the viewtime and lastview.
where did i go wrong now?
change this:
cmdC.CommandText = "Update ComDet set iCount = '" + z + "', ViewTime = '" + lblTime.Text + "', LastView = '" + txtUser2.Text + "' Where cName = '" + ListU.SelectedValue.ToString() + "'";
to
cmdC.CommandText = "Update ComDet set iCount = " + z + ", ViewTime = '" + lblTime.Text + "', LastView = '" + txtUser2.Text + "' Where cName = '" + ListU.SelectedValue.ToString() + "'";
you dont need the "'" apostrophe around it becuase its a number. That would definitely get you string not in correct format error
I would guess maybe the icount value is not a number, i would recommend using TryParse just in case. And that should keep this error from happening. What to do about a bad value getting returned by the query is another issue.
private void button4_Click(object sender, EventArgs e)
{
//need update code//
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
SqlDataAdapter daCount = new SqlDataAdapter("select iCount from ComDet where cName = #cName", conn);
daCount.SelectCommand.Parameters.Add("#cName", SqlDbType.VarChar).Value = ListU.SelectedValue;
DataTable dtC = new DataTable();
daCount.Fill(dtC);
DataRow firstRow = dtC.Rows[0];
string x = firstRow["iCount"].ToString();
int y = 0;
if(Int32.TryParse(x,out y))
{
System.Diagnostics.Debug.WriteLine("iCount was an valid int32");
int z = y + 1;
//SqlCeCommand cmdC = conn.CreateCommand();
SqlCommand cmdC = conn.CreateCommand();
cmdC.CommandText = "Update ComDet set iCount = " + z + ", ViewTime = '" + lblTime.Text + "', LastView = '" + txtUser2.Text + "' Where cName = '" + ListU.SelectedValue.ToString() + "'";
}
else
System.Diagnostics.Debug.WriteLine("iCount was NOT a valid int32, value: " + x);
conn.Close();
}
Have you checked the value of the 'x' variable? The exception informs that the value of X isn't a valid integer, so the FormatException is thrown.
guy how can i insert the value of checkbox in access database or any database.
i tried any of this sql statement but it still give me error: OleDbException was Unhandled. Data type mismatch in criteria expression. and it's pointing to myData = myCommand.ExecuteReader();
note that allowviewpsr is a boolean type of field in ms access database or the one with YES/NO. :) chkviewpsr is mycheckbox
SQL = "UPDATE `RUsers` SET `allowviewpsr` = '" + chkviewpsr.IsChecked.Value + "' WHERE `idnum`= '" + txtblkuserid.Text + "' AND `fullname`= '" + txtblkusername.Text + "'";
also this:
SQL = "UPDATE `RUsers` SET `allowviewpsr` = '" + chkviewpsr.IsChecked + "' WHERE `idnum`= '" + txtblkuserid.Text + "' AND `fullname`= '" + txtblkusername.Text + "'";
and also this:
SQL = "UPDATE `RUsers` SET `allowviewpsr` = '" + chkviewpsr + "' WHERE `idnum`= '" + txtblkuserid.Text + "' AND `fullname`= '" + txtblkusername.Text + "'";
and here's my connector:
myCommand.CommandText = SQL;
myCommand.Connection = MyNewOleDbConnection;
myAdapter.SelectCommand = myCommand;
myData = myCommand.ExecuteReader();
EDITED:
hi anandkumar thanks for the quick replay i tried NonQuery but it gives same error as above
SQL = "UPDATE `RWMUsers` SET `allowviewpsr` = '" + chkviewpsr.IsChecked.Value + "' WHERE `idnum`= '" + txtblkuserid.Text + "' AND `fullname`= '" + txtblkusername.Text + "'";
myCommand.CommandText = SQL;
myCommand.Connection = MyNewOleDbConnection;
myAdapter.UpdateCommand = myCommand;
myCommand.ExecuteNonQuery();
Snapshot of my Access Database :(
Instead of
myAdapter.SelectCommand = myCommand;
myCommand.ExecuteReader();
Use
myAdapter.UpdateCommand = myCommand;
myCommand.ExecuteNonQuery();
Reference:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.updatecommand.aspx