cannot implicitly convert type object to string

A

Andy Sutorius

Hi,

I am receiving the error when compiling the project, "cannot implicitly
convert type object to string".

The error points to this line of code and underlines the
dtrRecipient["Email"]: objMailMessage.To = dtrRecipient["Email"];

I'm not sure why I am getting this error. Below is the complete code I am
working with.

private void EmailEm()
{
string intSelectedEmail = ddlChooseEmail.SelectedValue.ToString();
string sqlGetEmail = "SELECT content, subject FROM tblContent WHERE id =
" + intSelectedEmail;
SqlCommand cmdGetEmail = new SqlCommand(sqlGetEmail, strConnString);
SqlDataReader dtrEmail = cmdGetEmail.ExecuteReader();

string intSelectedGroup = chkGroups.SelectedValue.ToString();
string sqlRECIPIENTS = "SELECT tblRecipients.Email AS Email FROM tblGR
INNER JOIN tblRecipients ON tblGR.RecipientID = tblRecipients.ID WHERE
tblGR.GroupID=" + intSelectedGroup;
SqlCommand cmdSelect = new SqlCommand( sqlRECIPIENTS, strConnString );
SqlDataReader dtrRecipient = cmdSelect.ExecuteReader();

MailMessage objMailMessage;

while (dtrRecipient.Read())
{
// Create the Mail Message
objMailMessage = new MailMessage();
objMailMessage.From = "";
objMailMessage.To = dtrRecipient["Email"];
objMailMessage.Subject = dtrEmail["Subject"];
objMailMessage.Body = dtrEmail["Content"];

// Send the Mail Message
SmtpMail.Send( objMailMessage );
}
}


Thanks,

Andy
 
K

Karl Seguin

Andy,
You simply can covert something of type System.Object to something of type
System.String. The "Email" column might be a varchar, but dr["xxx"] always
returns an object. you must Convert it to the type you wish:

objMailMessage.To = Convert.ToString(dtrRecipient["Email"]);

If you were using vb before, you could get away from doing this by having
Option Strict Off - but then your in for all sorts of trouble..

Karl
 
K

Kevin Spencer

Let me guess: You used to be a VB programmer, right?

When you use the indexer for a SqlDataReader, you are invoking the "Item"
method, which returns a type of "Object." As an object is not a string, you
can't simply assign it to a string. That would be an implicit conversion.
You have to either do an explicit conversion (using Convert) or, better yet,
use the approproate SqlDataReader methods for getting the correct type of
data out of the row, such as .GetString().

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
A

Andy Sutorius

Guilty of being a vb programmer :)

Thanks to both of you for your help!

Andy


Kevin Spencer said:
Let me guess: You used to be a VB programmer, right?

When you use the indexer for a SqlDataReader, you are invoking the "Item"
method, which returns a type of "Object." As an object is not a string, you
can't simply assign it to a string. That would be an implicit conversion.
You have to either do an explicit conversion (using Convert) or, better yet,
use the approproate SqlDataReader methods for getting the correct type of
data out of the row, such as .GetString().

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Neither a follower nor a lender be.

Andy Sutorius said:
Hi,

I am receiving the error when compiling the project, "cannot implicitly
convert type object to string".

The error points to this line of code and underlines the
dtrRecipient["Email"]: objMailMessage.To = dtrRecipient["Email"];

I'm not sure why I am getting this error. Below is the complete code I am
working with.

private void EmailEm()
{
string intSelectedEmail = ddlChooseEmail.SelectedValue.ToString();
string sqlGetEmail = "SELECT content, subject FROM tblContent WHERE id =
" + intSelectedEmail;
SqlCommand cmdGetEmail = new SqlCommand(sqlGetEmail, strConnString);
SqlDataReader dtrEmail = cmdGetEmail.ExecuteReader();

string intSelectedGroup = chkGroups.SelectedValue.ToString();
string sqlRECIPIENTS = "SELECT tblRecipients.Email AS Email FROM tblGR
INNER JOIN tblRecipients ON tblGR.RecipientID = tblRecipients.ID WHERE
tblGR.GroupID=" + intSelectedGroup;
SqlCommand cmdSelect = new SqlCommand( sqlRECIPIENTS, strConnString );
SqlDataReader dtrRecipient = cmdSelect.ExecuteReader();

MailMessage objMailMessage;

while (dtrRecipient.Read())
{
// Create the Mail Message
objMailMessage = new MailMessage();
objMailMessage.From = "";
objMailMessage.To = dtrRecipient["Email"];
objMailMessage.Subject = dtrEmail["Subject"];
objMailMessage.Body = dtrEmail["Content"];

// Send the Mail Message
SmtpMail.Send( objMailMessage );
}
}


Thanks,

Andy
 
K

Karl Seguin

Doesn't .GetString() require an ordinal position, which, in my opinion
wouldn't make it a "better" choice, just an alternative one with its own
pros and cons

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Kevin Spencer said:
Let me guess: You used to be a VB programmer, right?

When you use the indexer for a SqlDataReader, you are invoking the "Item"
method, which returns a type of "Object." As an object is not a string, you
can't simply assign it to a string. That would be an implicit conversion.
You have to either do an explicit conversion (using Convert) or, better yet,
use the approproate SqlDataReader methods for getting the correct type of
data out of the row, such as .GetString().

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Neither a follower nor a lender be.

Andy Sutorius said:
Hi,

I am receiving the error when compiling the project, "cannot implicitly
convert type object to string".

The error points to this line of code and underlines the
dtrRecipient["Email"]: objMailMessage.To = dtrRecipient["Email"];

I'm not sure why I am getting this error. Below is the complete code I am
working with.

private void EmailEm()
{
string intSelectedEmail = ddlChooseEmail.SelectedValue.ToString();
string sqlGetEmail = "SELECT content, subject FROM tblContent WHERE id =
" + intSelectedEmail;
SqlCommand cmdGetEmail = new SqlCommand(sqlGetEmail, strConnString);
SqlDataReader dtrEmail = cmdGetEmail.ExecuteReader();

string intSelectedGroup = chkGroups.SelectedValue.ToString();
string sqlRECIPIENTS = "SELECT tblRecipients.Email AS Email FROM tblGR
INNER JOIN tblRecipients ON tblGR.RecipientID = tblRecipients.ID WHERE
tblGR.GroupID=" + intSelectedGroup;
SqlCommand cmdSelect = new SqlCommand( sqlRECIPIENTS, strConnString );
SqlDataReader dtrRecipient = cmdSelect.ExecuteReader();

MailMessage objMailMessage;

while (dtrRecipient.Read())
{
// Create the Mail Message
objMailMessage = new MailMessage();
objMailMessage.From = "";
objMailMessage.To = dtrRecipient["Email"];
objMailMessage.Subject = dtrEmail["Subject"];
objMailMessage.Body = dtrEmail["Content"];

// Send the Mail Message
SmtpMail.Send( objMailMessage );
}
}


Thanks,

Andy
 
K

Kevin Spencer

Doesn't .GetString() require an ordinal position, which, in my opinion
wouldn't make it a "better" choice, just an alternative one with its own
pros and cons

Well, Karl, I'm sure that's debateable. Yes, it does require an Ordinal
position, and I generally use it with GetOrdinal(), as in:

objMailMessage.To =
dtrRecipient.GetString(dtrRecipient.GetOrdinal("Email"));

If you're going to use the same column more than once, you can set an
integer variable for the ordinal to save some cycles.

As an old C programmer, I tend to avoid anything that works with Object.
However, on reflection, I couldn't say for sure which was more efficient at
run-time. I would think that Conversion functions have higher overhead, but
I have no proof.


Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.

Karl Seguin said:
Doesn't .GetString() require an ordinal position, which, in my opinion
wouldn't make it a "better" choice, just an alternative one with its own
pros and cons

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Kevin Spencer said:
Let me guess: You used to be a VB programmer, right?

When you use the indexer for a SqlDataReader, you are invoking the "Item"
method, which returns a type of "Object." As an object is not a string, you
can't simply assign it to a string. That would be an implicit conversion.
You have to either do an explicit conversion (using Convert) or, better yet,
use the approproate SqlDataReader methods for getting the correct type of
data out of the row, such as .GetString().

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Neither a follower nor a lender be.

Andy Sutorius said:
Hi,

I am receiving the error when compiling the project, "cannot implicitly
convert type object to string".

The error points to this line of code and underlines the
dtrRecipient["Email"]: objMailMessage.To = dtrRecipient["Email"];

I'm not sure why I am getting this error. Below is the complete code I am
working with.

private void EmailEm()
{
string intSelectedEmail = ddlChooseEmail.SelectedValue.ToString();
string sqlGetEmail = "SELECT content, subject FROM tblContent WHERE
id =
" + intSelectedEmail;
SqlCommand cmdGetEmail = new SqlCommand(sqlGetEmail, strConnString);
SqlDataReader dtrEmail = cmdGetEmail.ExecuteReader();

string intSelectedGroup = chkGroups.SelectedValue.ToString();
string sqlRECIPIENTS = "SELECT tblRecipients.Email AS Email FROM
tblGR
INNER JOIN tblRecipients ON tblGR.RecipientID = tblRecipients.ID WHERE
tblGR.GroupID=" + intSelectedGroup;
SqlCommand cmdSelect = new SqlCommand( sqlRECIPIENTS,
strConnString );
SqlDataReader dtrRecipient = cmdSelect.ExecuteReader();

MailMessage objMailMessage;

while (dtrRecipient.Read())
{
// Create the Mail Message
objMailMessage = new MailMessage();
objMailMessage.From = "";
objMailMessage.To = dtrRecipient["Email"];
objMailMessage.Subject = dtrEmail["Subject"];
objMailMessage.Body = dtrEmail["Content"];

// Send the Mail Message
SmtpMail.Send( objMailMessage );
}
}


Thanks,

Andy
 
I

Iain Norman

Well, Karl, I'm sure that's debateable. Yes, it does require an Ordinal
position, and I generally use it with GetOrdinal(), as in:

objMailMessage.To =
dtrRecipient.GetString(dtrRecipient.GetOrdinal("Email"));

If you're going to use the same column more than once, you can set an
integer variable for the ordinal to save some cycles.

As an old C programmer, I tend to avoid anything that works with Object.
However, on reflection, I couldn't say for sure which was more efficient at
run-time. I would think that Conversion functions have higher overhead, but
I have no proof.


Kevin Spencer
Microsoft MVP
.Net Developer
Neither a follower nor a lender be.

Would I be correct in saying the ordinal is faster though? I tend to
use ordinals in comination with some column name constants.

I
 
K

Kevin Spencer

I would think that if you use a typed method such as GetString() with a
literal or variable Ordinal, it would be faster. Again, I have no proof. But
there is no conversion involved, so I would tend to think it was faster.
What I was doubtful about was whether using a typed method with the
GetOrdinal() method would be faster, as in:

objMailMessage.To =
dtrRecipient.GetString(dtrRecipient.GetOrdinal("Email"));

This requires 2 method calls for each column.

Still, the GetOrdinal() method just loops through the columns to find one
with the name specified, so it can't be too expensive. I just can't say with
any authority whether the 2 method calls to get the typed value are more
efficient than the 2 method calls using Convert (1 call to the Item()
method, plus 1 call to Convert). My guts tell me that the first alternative
would be faster, since Conversion functions have to use Reflection to derive
the type of the object.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
K

Karl Seguin

I have seen test about this, and GetXXX is faster when looping through
records but slower when doing a single record. Can't find the link though
:(

The point I was trying to make is that the performance difference is
incredibly small in both cases for most typical usage (I mean, if ur looping
through 1000 records, then it might start to be a factor, and this probably
isn't at all uncomon, but I still wouldn't call it "typical"). I believe,
with such a small difference, the rule should be maintainability, the
exception performance.

GetXXX(y) by itself, in my opinion, is pretty bad for maintenance. (a) you
don't know what field it's actually getting, (b) it can easily break.
using GetXXX with GetOrdinal however is pretty much are maintainable as the
explicit column names, so the performance benefits are probably key... In
the initial post Kevin didn't make mention of GetOrdinal which I guess is
why I though it necessary to raise a flag (besides, I've never been invovled
in one of Kevin's long threads and thought this could be my chance!)

Karl
 
K

Kevin Spencer

Karl,

Your logic is impeccable. :)

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top