1

I have a datagridview that bind with a datatable and using DataPropertyName the datagridview only shows 4 columns of database.
this is how I bind database to a datagridview:

string StrCon = System.Configuration.ConfigurationManager.ConnectionStrings
["FeedLibraryConnectionString"].ConnectionString;
OleDbConnection Connection = new OleDbConnection(StrCon);
OleDbDataAdapter DataA = new OleDbDataAdapter("Select * from SelectedFeeds", Connection);
DataTable DTable = new DataTable();
DataA.Fill(DTable);

frm.RationFeedsdataGridView.AutoGenerateColumns = false;

frm.RationFeedsdataGridView.Columns[1].Name = "نام خوراک";
frm.RationFeedsdataGridView.Columns[1].HeaderText = "نام خوراک";
frm.RationFeedsdataGridView.Columns[1].DataPropertyName = "Feed Name / Description";

frm.RationFeedsdataGridView.Columns[2].Name = "مقدار (کیلوگرم)";
frm.RationFeedsdataGridView.Columns[2].HeaderText = "مقدار (کیلوگرم)";
frm.RationFeedsdataGridView.Columns[2].DataPropertyName = "Quantity";

frm.RationFeedsdataGridView.Columns[3].Name = "درصد (خوراک)";
frm.RationFeedsdataGridView.Columns[3].HeaderText = "درصد (خوراک)";
frm.RationFeedsdataGridView.Columns[3].DataPropertyName = "Percent";

frm.RationFeedsdataGridView.DataSource = DTable;

So far nothing wrong,

now I want when user change the value of Columns[2], it's value automatically inserted (and after that updated) into database I tryed myself using this code but seems that it's wrong, the code is:

string StrCon = System.Configuration.ConfigurationManager.ConnectionStrings
["FeedLibraryConnectionString"].ConnectionString;
OleDbConnection Connection = new OleDbConnection(StrCon);
OleDbCommand Cmd = new OleDbCommand();
Cmd.Connection = Connection;
double Quantity = Convert.ToDouble(frm.RationFeedsdataGridView.CurrentCell.Value);
string FeedName = frm.RationFeedsdataGridView.CurrentRow.Cells[1].Value.ToString();
Connection.Open();
Cmd.CommandText="Update SelectedFeeds set Quantity=" +Quantity  'Where Feed Name / Description'= FeedName;
Cmd.ExecuteNonQuery();
Connection.Close();

the first problem is that my CommandText is wrong and I don't know how to fix it. what should I do?

4
  • Always use parameters. If a field name has a space or is a keyword, you have to put brackets around it, so [Feed Name / Description]. BTW, that's a horrible column name. Commented May 11, 2016 at 17:22
  • As larstexh said you should use paramatera as your quotatuons are wrong Commented May 11, 2016 at 17:47
  • @LarsTech tnx, can you explain it to me how I use parameters? Commented May 11, 2016 at 17:57
  • 1
    See Adding Parameters ... Commented May 11, 2016 at 18:08

1 Answer 1

4

tanks to @LarsTech the answer is:

public static void UpdateRationFeeds(Form1 frm)
{
    string StrCon = System.Configuration.ConfigurationManager.ConnectionStrings["FeedLibraryConnectionString"].ConnectionString;
    OleDbConnection Connection = new OleDbConnection(StrCon);
    OleDbCommand Cmd = new OleDbCommand();
    Cmd.Connection = Connection;
    double Quantity = Convert.ToDouble(frm.RationFeedsdataGridView.CurrentCell.Value);
    string FeedName = frm.RationFeedsdataGridView.CurrentRow.Cells[1].Value.ToString();
    Connection.Open();
    Cmd.CommandText = "Update SelectedFeeds set Quantity=@quantity Where SelectedFeeds.[Feed Name]=@feedname";
    Cmd.Parameters.Add("@quantity", OleDbType.Double).Value = Quantity;
    Cmd.Parameters.Add("@feedname", OleDbType.LongVarChar).Value = FeedName;
    Cmd.ExecuteNonQuery();
    Connection.Close();
}
Sign up to request clarification or add additional context in comments.

1 Comment

set Quantity = @quantity where... then Cmd.Parameters.Add("@quantity", ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.