Solved

Sql statment correct?

Posted on 2009-05-15
12
237 Views
Last Modified: 2012-05-07
I get an error on this:
"UPDATE t_account SET blance = @summa WHERE accountid = @id"

There isnt any value for one or more parameters is the error


string sql2 = "UPDATE t_account SET blance = @summa WHERE accountid = @id";
            
            OleDbConnection cn = new OleDbConnection(myConnectionString);
            using (OleDbCommand cmd = new OleDbCommand(sql2, cn))
            {
 
                cmd.Parameters.Add("@id", OleDbType.Integer).Value = id;
                cmd.Parameters.Add("@summa", OleDbType.Integer).Value = summa;           
 
                cn.Open();
                cmd.ExecuteNonQuery();
                cn.Close();
            }

Open in new window

0
Comment
Question by:Mickeys
[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
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 100 total points
ID: 24394573
try this
 string sql2 = "UPDATE t_account SET blance =" + summa + "WHERE accountid =" + id;
 
            OleDbConnection cn = new OleDbConnection(myConnectionString);
            using (OleDbCommand cmd = new OleDbCommand(sql2, cn))
            {
                 cn.Open();
                cmd.ExecuteNonQuery();
                cn.Close();
            }

Open in new window

0
 
LVL 39

Accepted Solution

by:
abel earned 200 total points
ID: 24394643
So not go for the SQL string concatenation proposed in the first answer, that will make you liable for SQL injection attacks. You should stay with the Parameter approach at all times (or further abstractions like ORMs offer)

You are using the @-sign in the Parameters.Add. You should remove it:

string sql2 = "UPDATE t_account SET blance = @summa WHERE accountid = @id";
            
OleDbConnection cn = new OleDbConnection(myConnectionString);
using (OleDbCommand cmd = new OleDbCommand(sql2, cn))
{ 
    cmd.Parameters.Add("id", OleDbType.Integer).Value = id;
    cmd.Parameters.Add("summa", OleDbType.Integer).Value = summa;           
    cn.Open();
    cmd.ExecuteNonQuery();
    cn.Close();
}

Open in new window

0
 
LVL 39

Expert Comment

by:abel
ID: 24394651
So == Do (first word)
0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 

Author Comment

by:Mickeys
ID: 24394965
I havent tried pratima_mcs: suggesstion since that is the incorrect way to go.

Abel: That dindt help. Same error
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 24395003
try this

I think you missed
cmd.CommandType = CommandType.Text;
string sql2 = "UPDATE t_account SET blance = @summa WHERE accountid = @id";
            
            OleDbConnection cn = new OleDbConnection(myConnectionString);
            using (OleDbCommand cmd = new OleDbCommand(sql2, cn))
            {
 cmd.CommandType = CommandType.Text; 
                cmd.Parameters.Add("@id", OleDbType.Integer).Value = id;
                cmd.Parameters.Add("@summa", OleDbType.Integer).Value = summa;           
 
                cn.Open();
                cmd.ExecuteNonQuery();
                cn.Close();
            }

Open in new window

0
 

Author Comment

by:Mickeys
ID: 24395118
no Same error. :-/

There are more columns in the table but I dont need to have them in the sql statment when I am just gonna update one of the columns, right?
0
 

Author Comment

by:Mickeys
ID: 24395131
Here are the database. It is t_account.

database.bmp
0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 200 total points
ID: 24395148
try this one
http://www.java2s.com/Code/ASP/ADO.net-Database/UsedirectSQLtoinsertC.htm
            string sql2 = "UPDATE t_account SET blance = @summa WHERE accountid = @id";
            
            OleDbConnection cn = new OleDbConnection(myConnectionString);
            cn.Open();
            using (OleDbCommand cmd = new OleDbCommand(sql2, cn))
            {
                cmd.CommandType = CommandType.Text; 
                cmd.Parameters.Add("@id", OleDbType.Integer).Value = id;
                cmd.Parameters.Add("@summa", OleDbType.Integer).Value = summa;
                cmd.ExecuteNonQuery();
                cn.Close();
            }

Open in new window

0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 24395168
yes ..correct
0
 

Author Comment

by:Mickeys
ID: 24395418
sorry same error. I have used exactly that code for SELECT and INSERT and that works.
0
 
LVL 39

Expert Comment

by:abel
ID: 24396240
Think that by now it is time to see what is generated, actually. There is a mismatch between a sql parameter and the name you give it. In the code that you show, it isn't there. Is it possible that the actual query is larger?

Can you apply a Trace on the SQL server to see what the actual code is that is sent to the server and paste that here?
0
 

Author Closing Comment

by:Mickeys
ID: 31581882
and the error is found. wrong spelling on the column name balance
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

730 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