0

I have a datagridview that it's columnNames are equal to my columns in access database

Now I want this: whene user click on savebtn all of datagridview columns values automatically updated to my database,
I've tried this code but it get me syntax error :

{"Syntax error (missing operator) in query expression '@1Where FeedID=@feedID'."}

where is the problem?

public void UpdatetFeeds(DataGridView dgv)
{
    string StrCon = System.Configuration.ConfigurationManager.ConnectionStrings
    ["FeedLibraryConnectionString"].ConnectionString;

    int FeedID = Convert.ToInt32(dgv.CurrentRow.Cells[0].Value);

    using (OleDbConnection Connection = new OleDbConnection(StrCon))
    {
        Connection.Open();

        using (OleDbCommand Cmd = new OleDbCommand())
        {
            Cmd.Connection = Connection;

            foreach (DataGridViewColumn column in dgv.Columns)
            {
                string columnName = dgv.Columns[column.Index].Name;
                object columnValue = dgv.CurrentRow.Cells[column.Index].Value;

                Cmd.CommandText = @"Update tFeeds Set " + columnName + " =@"
                    + columnValue + "Where FeedID=@feedID";

                Cmd.Parameters.Add("@feedID", OleDbType.Integer).Value = FeedID;


                Cmd.ExecuteNonQuery();
            }
        }
     }
 }

1 Answer 1

1

Seems at least you lost space

" Where FeedID=@feedID";

Also, what kind of data you have here: " =@" + columnValue + ". Seems you need braskets here: " =@'" + columnValue + "'

Sign up to request clarification or add additional context in comments.

3 Comments

yes I lost that space and after that code executed, But instead of updating cell bu column value, cell updated with FeedID parameters value
data type are string, int and double, it's okay that I use a object? or not?
But you have only one parameter @feedId. If columnValue is string - you need bradkets. So brtter add one more parameter to avoid problems with data types. And use columnName = @feedValue. New param: Cmd.Parameters.Add("@feedValue", ...

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.