473,320 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Problem writing parametrized query


Hi!

On the recommendation of one of the MVP's on this group....... I tried
writing parametrized queries. But the fucking thing does not work and
it does not update the data in the table.
I gotta do my work by concatenation right now. But what is wrong with
the code anyway?

Can anyone figure out.

public static bool Change_CC_Pincode(string userName, string
Pincode)
{
string query = "UPDATE CreditCard SET cc_pincode=@PINCODE
WHERE username=@USERNAME";
SqlConnection cn = new SqlConnection(connectionString);
// Create a new SQL Command object with our query
// Note the syntax for our parameter field, "first"
SqlCommand sqlCommand = new SqlCommand(query, cn);

sqlCommand.Parameters.Add("@PINCODE", SqlDbType.Int).Value
= Pincode;
sqlCommand.Parameters.Add("@USERNAME",
SqlDbType.VarChar).Value =userName.ToCharArray();
cn.Open();
int result = sqlCommand.ExecuteNonQuery();

// Close Reader and Connection.
cn.Close();
if (result 0)
return true;
else
return false;

}

Regards
Generous for your help

Apr 17 '07 #1
2 1717
On Apr 17, 11:48 am, weird0 <amiredi...@gmail.comwrote:
Hi!

On the recommendation of one of the MVP's on this group....... I tried
writing parametrized queries. But the fucking thing does not work and
it does not update the data in the table.
I gotta do my work by concatenation right now. But what is wrong with
the code anyway?

Can anyone figure out.

public static bool Change_CC_Pincode(string userName, string
Pincode)
{
string query = "UPDATE CreditCard SET cc_pincode=@PINCODE
WHERE username=@USERNAME";
SqlConnection cn = new SqlConnection(connectionString);

// Create a new SQL Command object with our query
// Note the syntax for our parameter field, "first"
SqlCommand sqlCommand = new SqlCommand(query, cn);

sqlCommand.Parameters.Add("@PINCODE", SqlDbType.Int).Value
= Pincode;
sqlCommand.Parameters.Add("@USERNAME",
SqlDbType.VarChar).Value =userName.ToCharArray();
cn.Open();
int result = sqlCommand.ExecuteNonQuery();

// Close Reader and Connection.
cn.Close();
if (result 0)
return true;
else
return false;

}

Regards
Generous for your help
In the code I have written I have not used the @ sign in the variable
names when adding the parameters and it has worked. For example, drop
the @ in the line

sqlCommand.Parameters.Add("@PINCODE", SqlDbType.Int).Value = Pincode;

to

sqlCommand.Parameters.Add("PINCODE", SqlDbType.Int).Value = Pincode;

Perhaps it will work or atleast get you to your next error!

Apr 17 '07 #2
public static bool Change_CC_Pincode(string userName, string
Pincode)
{
SqlConnection conn = null;
int result = -1;
try
{
conn = new SqlConnection(connectionString);
SqlCommand command = conn.CreateCommand();
command.CommandText = "UPDATE CreditCard SET
cc_pincode=@PINCODE WHERE
username=@USERNAME";

command.Parameters.AddWithValue("@PINCODE", Pincode);
command.Parameters.AddWithValue("@USERNAME" userName);
conn.Open();

result = command.ExecuteNonQuery();

}
catch( Exception ex )
{
//woopsie
}
finally
{
if( conn != null ) conn.Close();
return result 0;
}
}

Apr 18 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: jslowery | last post by:
Hello, I'm new to both PostgreSQL and psycopg and I'm trying to connect to my database running on localhost. I have postgres setup to do md5 authentication and this works when using a db admin tool...
3
by: rwawryk | last post by:
Hi, Does anybody know how to implement parametrized stream operator (such as setw, setfill)? I need to put into the stream variable of type char* without terminating NULL. It would be great if I...
22
by: nobody | last post by:
hello everybody, is there a way of creating an array with help of a function that would accept the name of this array as a parameter and then create global Array type variable of that name? so...
1
by: andrew queisser | last post by:
I've been trying to dynamically create a class DevT that's derived from a generic base GenBase<T>. It doesn't seem to work. I'm attaching a code sample below that illustrates the problem. ...
7
by: jsale | last post by:
I'm currently using ASP.NET with VS2003 and SQL Server 2003. The ASP.NET app i have made is running on IIS v6 and consists of a number of pages that allow the user to read information from the...
1
by: jagdishl | last post by:
Hi: I have used the following code for my update query button I have used VB.Net Private Sub btncollections_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles...
0
by: maxvalery | last post by:
Hi, I have a DTS package for SQL Server 2000. Its entire purpose is to 1) run a parametrized stored procedure 2) run a query 3) EXPORT parametrized stored procedure into an Excel file NAMED...
4
by: mario.rossi | last post by:
Hi all, I am trying to invoke the default constructor from another, parametrized, constructor, but the default constructor doesn't get invoked at all, I saw. Is this correct ISO C++ behaviour or...
8
by: John | last post by:
Hi I am using the below code to run a sql on an underlying access table; insStr = "INSERT INTO ( Action, , , Request_Date ) " & _ "VALUES (""Modify Client"", 93, ""Administrator"", Now())"...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.