473,785 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_Pinco de(string userName, string
Pincode)
{
string query = "UPDATE CreditCard SET cc_pincode=@PIN CODE
WHERE username=@USERN AME";
SqlConnection cn = new SqlConnection(c onnectionString );
// Create a new SQL Command object with our query
// Note the syntax for our parameter field, "first"
SqlCommand sqlCommand = new SqlCommand(quer y, cn);

sqlCommand.Para meters.Add("@PI NCODE", SqlDbType.Int). Value
= Pincode;
sqlCommand.Para meters.Add("@US ERNAME",
SqlDbType.VarCh ar).Value =userName.ToCha rArray();
cn.Open();
int result = sqlCommand.Exec uteNonQuery();

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

}

Regards
Generous for your help

Apr 17 '07 #1
2 1752
On Apr 17, 11:48 am, weird0 <amiredi...@gma il.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_Pinco de(string userName, string
Pincode)
{
string query = "UPDATE CreditCard SET cc_pincode=@PIN CODE
WHERE username=@USERN AME";
SqlConnection cn = new SqlConnection(c onnectionString );

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

sqlCommand.Para meters.Add("@PI NCODE", SqlDbType.Int). Value
= Pincode;
sqlCommand.Para meters.Add("@US ERNAME",
SqlDbType.VarCh ar).Value =userName.ToCha rArray();
cn.Open();
int result = sqlCommand.Exec uteNonQuery();

// 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.Para meters.Add("@PI NCODE", SqlDbType.Int). Value = Pincode;

to

sqlCommand.Para meters.Add("PIN CODE", 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_Pinco de(string userName, string
Pincode)
{
SqlConnection conn = null;
int result = -1;
try
{
conn = new SqlConnection(c onnectionString );
SqlCommand command = conn.CreateComm and();
command.Command Text = "UPDATE CreditCard SET
cc_pincode=@PIN CODE WHERE
username=@USERN AME";

command.Paramet ers.AddWithValu e("@PINCODE", Pincode);
command.Paramet ers.AddWithValu e("@USERNAME" userName);
conn.Open();

result = command.Execute NonQuery();

}
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
8013
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 on my local network. For some reason, psycopg fails with IDENT authentication. >>> import psycopg >>> psycopg.connect("dbname=jlowery user=jlowery host=localhost password=XXX") Traceback (most recent call last): File "<stdin>", line 1, in ?
3
3346
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 had the manipulator which allows to determine maximum number of characters ( let's assume maxw( char* str, int maxlen ) ). example: cout << maxw("Crocodile", 5 );
22
2762
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 that for example the following code would work as well in browsers as under Windows Scripting Host: str = "tableA";
1
2136
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. CreateType() fails when the base class is a parametrized class, as in DevT : GenBase<int>. CreateType() works if the base class is not parametrized, even if the base of the base was parametrized, as in DevT : GenBaseInt : GenBase<int>
7
2919
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 database into classes, which are used throughout the application. I have made class collections which, upon reading from the DB, create an instance of the class and store the DB values in there temporarily. My problem is that if user1 looks at...
1
1639
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 btncollections.Click Const connstr As String = "Provider=sqloledb;Data Source=Athena;Initial Catalog=OCACollections_DB;Integrated Security=SSPI;" Dim objConn As New OleDbConnection(connstr) Dim MYsql As String = "Insert into
0
1200
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 according to the query from #2 4) REPEAT until reached last row of the query, then run last row and finish DTS package.
4
1742
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 my compiler has a ++bug (overflowing)? struct Test { int x; int y; // Test() {
8
1525
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())" Dim insCmd As New OleDbCommand(insStr, dbConContacts)
0
10350
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7505
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.