asign a parameter command2

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I use the parameter command,

cmd.Parameters.Add("@var_new", OleDbType.Integer).Value= t_var;

I receive the error,

Compiler Error Message: CS0029: Cannot implicitly convert type 'int' to
'System.Data.OleDb.OleDbDataReader'

t_var is declared as an int. So what's the problem?
 
Yes, here is all the code,

string conVar = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\\data\\data;Extended Properties=dBase 5.0";
string conVarCommand= "Select var From mob_var";

OleDbConnection cn= new OleDbConnection();
cn.ConnectionString= conVar;
cn.Open();

OleDbCommand cmd = new OleDbCommand(conVarCommand, cn);
OleDbDataReader dtrVar = cmd.ExecuteReader();
string VarText;
if (dtrVar.Read()){
VarText= dtrVar["var"].ToString();
}
int t_var;
t_var = Convert.ToInt32(dtrVar["var"]);
t_var += 1;

conVarCommand= "Update mob_var Set var=@var_new";
cmd = new OleDbCommand(conVarCommand, cn);
cmd.Parameters.Add("@var_new", OleDbType.Integer).Value= t_var;
dtrVar = cmd.ExecuteNonQuery();
Response.Write("Var-"+t_var);
cn.Close();

Miha Markic said:
Can you show us a bit more code?

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

I use the parameter command,

cmd.Parameters.Add("@var_new", OleDbType.Integer).Value= t_var;

I receive the error,

Compiler Error Message: CS0029: Cannot implicitly convert type 'int' to
'System.Data.OleDb.OleDbDataReader'

t_var is declared as an int. So what's the problem?
 
Hi David,

First thing is that you really have to Close reader before using another
reader with the same connection.
The other thing is, that you should replace parameter name with question
mark:
conVarCommand= "Update mob_var Set var=@var_new";
change to
conVarCommand= "Update mob_var Set var=?";

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Yes, here is all the code,

string conVar = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\\data\\data;Extended Properties=dBase 5.0";
string conVarCommand= "Select var From mob_var";

OleDbConnection cn= new OleDbConnection();
cn.ConnectionString= conVar;
cn.Open();

OleDbCommand cmd = new OleDbCommand(conVarCommand, cn);
OleDbDataReader dtrVar = cmd.ExecuteReader();
string VarText;
if (dtrVar.Read()){
VarText= dtrVar["var"].ToString();
}
int t_var;
t_var = Convert.ToInt32(dtrVar["var"]);
t_var += 1;

conVarCommand= "Update mob_var Set var=@var_new";
cmd = new OleDbCommand(conVarCommand, cn);
cmd.Parameters.Add("@var_new", OleDbType.Integer).Value= t_var;
dtrVar = cmd.ExecuteNonQuery();
Response.Write("Var-"+t_var);
cn.Close();

Miha Markic said:
Can you show us a bit more code?

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

I use the parameter command,

cmd.Parameters.Add("@var_new", OleDbType.Integer).Value= t_var;

I receive the error,

Compiler Error Message: CS0029: Cannot implicitly convert type 'int' to
'System.Data.OleDb.OleDbDataReader'

t_var is declared as an int. So what's the problem?
 
After making the suggested changes I received the same error. Then I tried
totally closing the connection and then reopening it, and I received the same
error.

Miha Markic said:
Hi David,

First thing is that you really have to Close reader before using another
reader with the same connection.
The other thing is, that you should replace parameter name with question
mark:
conVarCommand= "Update mob_var Set var=@var_new";
change to
conVarCommand= "Update mob_var Set var=?";

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Yes, here is all the code,

string conVar = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\\data\\data;Extended Properties=dBase 5.0";
string conVarCommand= "Select var From mob_var";

OleDbConnection cn= new OleDbConnection();
cn.ConnectionString= conVar;
cn.Open();

OleDbCommand cmd = new OleDbCommand(conVarCommand, cn);
OleDbDataReader dtrVar = cmd.ExecuteReader();
string VarText;
if (dtrVar.Read()){
VarText= dtrVar["var"].ToString();
}
int t_var;
t_var = Convert.ToInt32(dtrVar["var"]);
t_var += 1;

conVarCommand= "Update mob_var Set var=@var_new";
cmd = new OleDbCommand(conVarCommand, cn);
cmd.Parameters.Add("@var_new", OleDbType.Integer).Value= t_var;
dtrVar = cmd.ExecuteNonQuery();
Response.Write("Var-"+t_var);
cn.Close();

Miha Markic said:
Can you show us a bit more code?

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

I use the parameter command,

cmd.Parameters.Add("@var_new", OleDbType.Integer).Value= t_var;

I receive the error,

Compiler Error Message: CS0029: Cannot implicitly convert type 'int' to
'System.Data.OleDb.OleDbDataReader'

t_var is declared as an int. So what's the problem?
 
Back
Top