Solved

How to update a MySQL table using C#?

Posted on 2009-07-15
4
420 Views
Last Modified: 2013-12-17

Hello group,

How can I update a record in MySQL table?
I can select and get the resuls but I cannot update any record.

Thanks.

strSQLStmt1 = "";
 strSQLStmt1 = "UPDATE table set  fld= '" + t.score.Trim().ToString() + "' WHERE Id = '" + t.applicant_id.Trim() + "' ";
                        Reader1 = command.ExecuteReader();
                        Reader1.Close();

Open in new window

0
Comment
Question by:akohan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 24865730
Use

   int rowsUpdated;
   rowsUpdated = command.ExecuteNonQuery();

instead of ExecuteReader(). rowsUpdated is just to show what is returned.
0
 

Author Comment

by:akohan
ID: 24865866


Thanks I'm sure that is the answer but I'm getting

Cannot implicitly convert type 'int' to 'MySql.Data.MySqlClient.MySqlDataReader'
0
 

Author Comment

by:akohan
ID: 24865879

OK I fixed that but my return value is -1 !

What could cause that? in msdn page says a rollback might cause it. Is this something I should take care of in db?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24868884
I don't see where in code you assigned the sql query to the command object. See notation below. Also, you can set a breakpoint at line 6 below, mouse over the strSQLStmtml variable and see the actual SQL statement that will be executed against the DB. You can try manually running that query against the DB to see if it generates any errors.
strSQLStmt1 = "";
strSQLStmt1 = "UPDATE table set  fld= '" + t.score.Trim().ToString() + "' WHERE Id = '" + t.applicant_id.Trim() + "' ";
 
command.CommandText = strSQLStmtl;
 
Reader1 = command.ExecuteReader();
Reader1.Close();

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Nested forach loop to linq 3 43
Office 365: Assigning MailboxPlan to a mailbox 8 72
aspx ascx, c# 7 43
Pdf not loading 2 40
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question