views:

45

answers:

3

Hello

Other time I need your help, I am developing an app in C# using an Access database(2007), the problem I think is the query updtate, I have searched over internet but nothing works, I have a datagridview and It has 3 columns the user needs to puts data to the last 2 columns(5 rows), I already do it but when I fill the columns in the datagridview It doenst store the data in the database access. So please I need you help this is my code, help very apreciated :D

private void btnGuardar_Click(object sender, EventArgs e)
    {

        //using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Databsename.accdb"))
        //{

        OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Databasename.accdb;Persist Security Info=false");


        int grade = 1;
        string x;
        string comment;

            for (int i = 0; i < this.dataGridView4.Rows.Count-1; i++)
            {

                x = dataGridView4.Rows[i].Cells[1].Value.ToString();
                //Console.WriteLine(x);
                MessageBox.Show(x);

                grade = int.Parse(x);


                comment = dataGridView4.Rows[i].Cells[2].Value.ToString();
                MessageBox.Show(comment);

                OleDbCommand cmd = new OleDbCommand("Update archievemnet set grade= @GRADE comment=@COMMENT WHERE idLine =1  ", conn);

                cmd.Parameters.Add(new OleDbParameter("@GRADE", OleDbType.Integer));
                cmd.Parameters["@GRADE"].Value = grade;

                cmd.Parameters.Add(new OleDbParameter("@comment", OleDbType.VarChar));
                cmd.Parameters["@COMMENT"].Value = comment;

                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }



        //}



    }
A: 

This is not the best solution, but you may try supplying the values directly in the query.

OleDbCommand cmd = new OleDbCommand("UPDATE archievemnet SET " + 
" grade = " + textBoxGrade.Text +
" comment = " + textBoxComment.Text +
" WHERE idLine = 1  ", conn);

cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();

EDIT:

OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Databasename.accdb;Persist Security Info=false");

int idLine;
double grade;
string comment;

for (int i = 0; i < this.dataGridView4.Rows.Count-1; i++)
{    
    idLine  = Convert.ToInt32(dataGridView4[0, i].Value);
    grade = Convert.ToDouble(dataGridView4[1, i].Value);
    comment = Convert.ToString(dataGridView4[2, i].Value);

    OleDbCommand cmd = new OleDbCommand("UPDATE archievemnet SET " + 
    " grade = " + grade +
    " comment = '" + comment + "' WHERE idLine = "+ idLine, conn);

    cmd.Connection.Open();
    cmd.ExecuteNonQuery();
    cmd.Connection.Close();
}
yonan2236
thanks for answering but I dont know how to take a specific colum of a datagridview, son I can no replace the values
Natalie
I already adapt my code to your solution, I copy your solution and it throws an exception that tell "the column idline is restricted to be unique, there is a prentent value 3 etc " thanks for answer I think that the error is in the sql query "OleDbCommand cmd = new OleDbCommand("Update archievemnet set grade= @GRADE comment=@COMMENT WHERE idLine =1 ", conn);" I write idLine = 1 just for prove that is inserting in the idline1 but there are other 4 idline , I think that there is an loop there but I try and nothing
Natalie
please try again my edit... I assumed that you idLine is type of int
yonan2236
yes wow I am surprised that you keep trying, really thanks for your time in answering but the exception is againg thrown
Natalie
hmm... if you don't mind, can you send me your app? I want to see the full code, or you just can post it somewhere for me to see.
yonan2236
I am from mexico, almost all the names of the database I translated to english for posting here so do you want to see? can I trust in you?
Natalie
Are the data sensitive? I have no bad intentions...just wanted to help
yonan2236
you can trust me...
yonan2236
what do you mean by data sensitive?
Natalie
is this just your assignment in school? In that case, I can help you.
yonan2236
yes It is like a homework
Natalie
ok, please send the needed files to this email: [email protected].
yonan2236
please indicate the subject as : "assignment"
yonan2236
sorry I dont want to waste you time but I can not give you this code, but thanks if I find a solution I will edit I promise :P
Natalie
ok... can you just provide a screen shot of your assignment? so i can visualize what you are trying to achieve.
yonan2236
what is your primary key on that table?
yonan2236
idLogro = idArchievement for the last 2 columns(I want to store in that table Archievement) but the fist column is binded to a table called LineaAccion =Line, its id is idLineaAccion= idLine(int) its retrieve data from a table called Line=LineaAccion
Natalie
is your dataGrid editable? I mean, you update the records on the grid? and the "Guardar" button means "Commit" or "Save" (something like that?) so whatever the changes on the grid will be updted once the button is clicked, right?
yonan2236
Please let me see your "SELECT" statement in populating that grid.
yonan2236
I am not using a select, is binded(BindingSource) so theres no code, is a tool from visual studio I dont know
Natalie
OK... but the data on that grid came from a single table?
yonan2236
yes just the first column, the other columns(other table) are filled because I already fill the dabase, that is the reason I am making an update
Natalie
what tables are affected on that particular grid?
yonan2236
just 1 table archievement(update to this table)
Natalie
do you have msn, because its night here and I need to sleep
Natalie
I only have a yahoo messenger account
yonan2236
ok just post it and we discussed later ok?
Natalie
You can contact me anytime using my email... Tomorrow you'll be awake, and I'm asleep. It would be better to send me your app, trust me. Besides, it's just a homework. (Unless that homework is a national security concern... hehe). Anyway, I'm just a click away. :)
yonan2236
whats your email? I already saw your profile
Natalie
yonan2236
what's your's?..
yonan2236
I will write you for me theres no problem
Natalie
what time is it in your place?
yonan2236
A: 

In your code you have "Update archievemnet ...". This is possibly the name of your table but the proper English word is "Achievement". Check that you are spelling the name of the table correctly. You should also have a try/catch block around your code to catch any exceptions thrown by Access or the db connectors, and some logging in the exception block. (I use and recommend NLog.) You say you are getting an exception, what type is it and what is the message?

I think you are better off using parameters in your final code (like you started doing), rather than string concatentation like yonan2236 is recommending - but it can sometimes help debugging. You might try:

    String updateStmt = "UPDATE archievemnet SET " + 
        " grade = " + grade +
        " comment = '" + comment + "' WHERE idLine = "+ idLine;
    OleDbCommand cmd = new OleDbCommand(updateStmt, conn);

and stop the code in the debugger just before you create the command. Copy the value of updateStmt into Access and see if you can execute the statement directly.

verisimilidude
I will debug, is just that it the first time that I work in C#, the exception is System.data.constraintexception the column idline is restricted to be unique, there is a prentent value 3
Natalie
This exception tells you that you are updating the table but the table is refusing your values. I think the problem is in your data definition.
verisimilidude
12:00 am in Mexico
Natalie
well I gotta go bye and thanks for all
Natalie
+1  A: 

Your UPDATE statement needs a comma before comment=

Look at this example which works on my system:

UPDATE tblFoo AS f SET f.parent_id = 99, f.foo_text = "updated"
WHERE (((f.id)=10));

Without the comma after 99 ...:

UPDATE tblFoo AS f SET f.parent_id = 99 f.foo_text = "updated"
WHERE (((f.id)=10));

... Access complains:

Syntax error (missing operator) in query expression '99 f.foo_text = "updated"'.

HansUp