Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: How to use parameters in ADOCommand


Message #1 by "Quynh D. Nguyen" <quynhnguyen@b...> on Wed, 14 Feb 2001 15:06:46
Hi all,

 

I have some code for insert a member's information into database. 



I used ADOCommand to excute SQL statement (hardcode) to insert data and all 

work fine.



But now, I like to use some parameters in ADOCommand but ASP+ server always 

show me the error below:



[Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared. 

[Microsoft][ODBC SQL Server Driver][SQL Server]Must declare the variable '@U...'. 

Description: An unhandled exception occurred at runtime during the 

execution of the current web request. Please review the stack trace below 

to get a better understanding of what the error is and where it originated 

from in the code. 



Exception Details: System.Data.ADO.ADOException: [Microsoft][ODBC SQL 

Server Driver][SQL Server]Statement(s) could not be prepared. [Microsoft]

[ODBC SQL Server Driver][SQL Server]Must declare the variable '@U...'. 







I may use ADOParameters in ADOCommand? Help me someone? 



My code is these:



====================================================



string strSQL = "Insert Into tmsTasks(UserID, CreateDate, Task, Status, 

Start, Stop, Duration, Interrupt, Description) ";



strSQL += "Values(@UserID, @CreateDate, @Task, 'New', null, null, 0, 0, 

@Description)";



cnnTimesheet = new ADOConnection(strConnectionString);



ADOCommand cmdTimesheet = new ADOCommand();



ADOParameter parTimesheet;



cmdTimesheet.ActiveConnection = cnnTimesheet;



cmdTimesheet.CommandText = strSQL;



cmdTimesheet.ActiveConnection.Open();



parTimesheet = new ADOParameter();



parTimesheet.DBType = ADODBType.Char;



parTimesheet.Size = 30;



parTimesheet.ParameterName = "UserID";



parTimesheet.Value = "FirstUser";



parTimesheet.Direction = ParameterDirection.Input;



parTimesheet.SourceColumn = "UserID";



parTimesheet.IsNullable = true;



cmdTimesheet.Parameters.Add(parTimesheet);



..........................



cmdTimesheet.Prepare();



cmdTimesheet.ExecuteNonQuery();



cmdTimesheet.ActiveConnection.Close();



 



Thanks for any ideas.



Quynh Nguyen

Message #2 by "Quynh D. Nguyen" <quynhnguyen@b...> on Thu, 15 Feb 2001 12:51:44 +0700
Hi,



Thank for your ideas but I' like to use ADOParameter (not SQLParameter)

because SQLParameter was designed only for SQL Server provider, unlike ADO.



I hope someday you will find a new solutions and help me to solve that.



Thanks.



Quynh Nguyen



-----Original Message-----

From: À¯ºÀÀç [mailto:bjyoo@G...]

Sent: Thursday, February 15, 2001 7:24 AM

To: quynhnguyen@b...

Subject: Fw: [aspx] How to use parameters in ADOCommand







                  string strConn;



                  strConn = "data source=BJYOO; initial catalog=dreamdb;

user id=sa;";



                  System.Data.SQL.SQLConnection MyConnection = new

SQLConnection(strConn);







                  //select * from KWANCODE where WORK_CODE = "2000001006"



                  string SelectCommand = "select * from KWANCODE where

WORK_CODE = @WORKCODE";



                  System.Data.SQL.SQLDataSetCommand myCommand 



new  SQLDataSetCommand( SelectCommand, MyConnection );







                  SQLParameter myParameter = new SQLParameter();



                  myParameter.ParameterName = "@WORKCODE";



                  myParameter.DataType = typeof(String);



                  myParameter.Size = 10;



                  myParameter.IsNullable = true;



                  myParameter.Value = TextBox1.Text.Trim(); // "2000001006"



                  myCommand.SelectCommand.Parameters.Add( myParameter );



                  System.Data.DataSet DS = new DataSet();



                  myCommand.FillDataSet( DS, "KWANCODE" );



                  DataGrid1.DataSource = DS.Tables["KWANCODE"].DefaultView;



                  DataGrid1.DataBind();









----- Original Message -----

From: "Quynh D. Nguyen" <quynhnguyen@b...>

To: "ASP+" <aspx@p...>

Sent: Wednesday, February 14, 2001 3:06 PM

Subject: [aspx] How to use parameters in ADOCommand





> Hi all,

>

> I have some code for insert a member's information into database.

>

> I used ADOCommand to excute SQL statement (hardcode) to insert data and

all

> work fine.

>

> But now, I like to use some parameters in ADOCommand but ASP+ server

always

> show me the error below:

>

> [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be

prepared.

> [Microsoft][ODBC SQL Server Driver][SQL Server]Must declare the variable

'@U...'.

> Description: An unhandled exception occurred at runtime during the

> execution of the current web request. Please review the stack trace below

> to get a better understanding of what the error is and where it originated

> from in the code.

>

> Exception Details: System.Data.ADO.ADOException: [Microsoft][ODBC SQL

> Server Driver][SQL Server]Statement(s) could not be prepared. [Microsoft]

> [ODBC SQL Server Driver][SQL Server]Must declare the variable '@U...'.

>

>

>

> I may use ADOParameters in ADOCommand? Help me someone?

>

> My code is these:

>

> ====================================================

>

> string strSQL = "Insert Into tmsTasks(UserID, CreateDate, Task, Status,

> Start, Stop, Duration, Interrupt, Description) ";

>

> strSQL += "Values(@UserID, @CreateDate, @Task, 'New', null, null, 0, 0,

> @Description)";

>

> cnnTimesheet = new ADOConnection(strConnectionString);

>

> ADOCommand cmdTimesheet = new ADOCommand();

>

> ADOParameter parTimesheet;

>

> cmdTimesheet.ActiveConnection = cnnTimesheet;

>

> cmdTimesheet.CommandText = strSQL;

>

> cmdTimesheet.ActiveConnection.Open();

>

> parTimesheet = new ADOParameter();

>

> parTimesheet.DBType = ADODBType.Char;

>

> parTimesheet.Size = 30;

>

> parTimesheet.ParameterName = "UserID";

>

> parTimesheet.Value = "FirstUser";

>

> parTimesheet.Direction = ParameterDirection.Input;

>

> parTimesheet.SourceColumn = "UserID";

>

> parTimesheet.IsNullable = true;

>

> cmdTimesheet.Parameters.Add(parTimesheet);

>

> ..........................

>

> cmdTimesheet.Prepare();

>

> cmdTimesheet.ExecuteNonQuery();

>

> cmdTimesheet.ActiveConnection.Close();

>

>

>

> Thanks for any ideas.

>

> Quynh Nguyen

>

Message #3 by "SATHISH C.G." <cgs@s...> on Thu, 15 Feb 2001 10:21:31 +0530
Hi,



        To execute a query in the database through vb.net with some

parameters first declare the parameters.The syntax for the declaration is





Dim cnd as ADOCommand



cmd=new ADOCommand("............",connection)



cmd.parameters.add("@userid" ,adodb.type)

..... Like wise declare as many parameters as u want....



cmd.parameters.value=some value



cmd.execute.





Alternatively we can also use the ADOParameters class to declare the

paramters seperatly an then we can append the parameters to an adocommand

object.The advantage of using this method is we can specify the direction of

the paramter.



I hope this will help U.





Regards



SATHISH C.G.









-----Original Message-----

From: Quynh D. Nguyen <quynhnguyen@b...>

To: ASP+ <aspx@p...>

Date: 14 February 2001 08:56

Subject: [aspx] How to use parameters in ADOCommand





>Hi all,

>

>I have some code for insert a member's information into database.

>

>I used ADOCommand to excute SQL statement (hardcode) to insert data and all

>work fine.

>

>But now, I like to use some parameters in ADOCommand but ASP+ server always

>show me the error below:

>

>[Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be

prepared.

>[Microsoft][ODBC SQL Server Driver][SQL Server]Must declare the variable

'@U...'.

>Description: An unhandled exception occurred at runtime during the

>execution of the current web request. Please review the stack trace below

>to get a better understanding of what the error is and where it originated

>from in the code.

>

>Exception Details: System.Data.ADO.ADOException: [Microsoft][ODBC SQL

>Server Driver][SQL Server]Statement(s) could not be prepared. [Microsoft]

>[ODBC SQL Server Driver][SQL Server]Must declare the variable '@U...'.

>

>

>

>I may use ADOParameters in ADOCommand? Help me someone?

>

>My code is these:

>

>====================================================

>

>string strSQL = "Insert Into tmsTasks(UserID, CreateDate, Task, Status,

>Start, Stop, Duration, Interrupt, Description) ";

>

>strSQL += "Values(@UserID, @CreateDate, @Task, 'New', null, null, 0, 0,

>@Description)";

>

>cnnTimesheet = new ADOConnection(strConnectionString);

>

>ADOCommand cmdTimesheet = new ADOCommand();

>

>ADOParameter parTimesheet;

>

>cmdTimesheet.ActiveConnection = cnnTimesheet;

>

>cmdTimesheet.CommandText = strSQL;

>

>cmdTimesheet.ActiveConnection.Open();

>

>parTimesheet = new ADOParameter();

>

>parTimesheet.DBType = ADODBType.Char;

>

>parTimesheet.Size = 30;

>

>parTimesheet.ParameterName = "UserID";

>

>parTimesheet.Value = "FirstUser";

>

>parTimesheet.Direction = ParameterDirection.Input;

>

>parTimesheet.SourceColumn = "UserID";

>

>parTimesheet.IsNullable = true;

>

>cmdTimesheet.Parameters.Add(parTimesheet);

>

>..........................

>

>cmdTimesheet.Prepare();

>

>cmdTimesheet.ExecuteNonQuery();

>

>cmdTimesheet.ActiveConnection.Close();

>

>

>

>Thanks for any ideas.

>

>Quynh Nguyen

>

>


  Return to Index