Parameter problem

J

Jim McGivney

Using VS'05 I have a label (LabelFileNum) on an aspx page. I want to set
the text of the lable to the last name, which is a field from a table called
Ante_pedigree from an Access Database.

I use the C# code-behind:



OleDbConnection dbConn = null;

OleDbCommand dCmd = null;

string strConn = null;

string strSQL = null;



// Open data connaction to get name info

strConn =
System.Configuration.ConfigurationManager.AppSettings["oledbConnStr"];

dbConn = new OleDbConnection(strConn);

dbConn.Open();



//Initialize label

LabelFileNum.Text = "";



// Get the last

strSQL = "SELECT LastName FROM Ante_Pedigree Where @AnteNum = ?";

dCmd = new OleDbCommand(strSQL, dbConn);

dCmd.Parameters.AddWithValue("@AnteNum", OleDbType.Char).Value = fileid;

LabelFileNum.Text = LabelFileNum.Text + " " +

dCmd.ExecuteScalar().ToString();



I get the error "No value given for one or more required parameters."

Any suggestions ? This similar code worked in asp.net 1.1

Thanks,

Jim
 
T

Trevor Braun

I've seen some strange results from lines like:
dCmd.Parameters.AddWithValue("@AnteNum", OleDbType.Char).Value = fileid;

try breaking it into two lines as you are actually trying to accomplish two
tasks:
dCmd.Parameters.AddWithValue("@AnteNum", OleDbType.Char);
dCmd.Parameters["@AnteNum"].Value = fileid;
(or at least I think that's the syntax)

That may help,
Trevor Braun



Jim McGivney said:
Using VS'05 I have a label (LabelFileNum) on an aspx page. I want to set
the text of the lable to the last name, which is a field from a table
called Ante_pedigree from an Access Database.

I use the C# code-behind:



OleDbConnection dbConn = null;

OleDbCommand dCmd = null;

string strConn = null;

string strSQL = null;



// Open data connaction to get name info

strConn =
System.Configuration.ConfigurationManager.AppSettings["oledbConnStr"];

dbConn = new OleDbConnection(strConn);

dbConn.Open();



//Initialize label

LabelFileNum.Text = "";



// Get the last

strSQL = "SELECT LastName FROM Ante_Pedigree Where @AnteNum = ?";

dCmd = new OleDbCommand(strSQL, dbConn);

dCmd.Parameters.AddWithValue("@AnteNum", OleDbType.Char).Value = fileid;

LabelFileNum.Text = LabelFileNum.Text + " " +

dCmd.ExecuteScalar().ToString();



I get the error "No value given for one or more required parameters."

Any suggestions ? This similar code worked in asp.net 1.1

Thanks,

Jim
 
N

Neutrino

"SELECT LastName FROM Ante_Pedigree Where @AnteNum = ?"

There you are comparing @AnteNum with ? (both seem to be parameters).

Because the ? is already a parameter then the right way should be:

"SELECT LastName FROM Ante_Pedigree Where AnteNum = ?"

This is how it would be with an SQLCommand:

"SELECT LastName FROM Ante_Pedigree Where AnteNum = @AnteNum"

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top