Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Specified Cast is not Valid

0 views
Skip to first unread message

U.@discussions.microsoft.com Marty U.

unread,
Sep 1, 2004, 8:01:04 AM9/1/04
to
First I am switching from VB.Net to C# so I am a little rusty on syntax.

I have a class with a couple methods and trying to assign a datareader value
to a variable which also has a public accessor for it. Below is a snippet and
where the problem occurs.

//Declare private variable

string name;

public string Name {
get {
return name;
}
set {
name = value;
}
}

I have a method that executes a SQL Server stored procedure returning the
value of a column with type varchar. Now I have done enough reading to
understand the GetSqlString and since I have a couple other columns such as
int from SQL server I need to find out why I am getting specified cast is not
valid when I do

SqlDataReader dtrMyReader;
dtrMyReader = myCommand.ExecuteReader();
while (dtrMyReader.Read())
{
this.name = dtrMyReader.GetSqlString(1);
}

this will not compile to an assembly because it gives me the error that
"Cannot implicitly convert type 'System.Data.SqlTypes.SqlString' to 'string'.
Which has to do with assigning the SqlString to the local variable "name". I
have the same problem with SQLServer integer field and probably the rest of
them to. I have found the datatype cross reference table that shows the .Net
data types vs the sql data types.

Can someone please give me some insight as to what I need to change to get
this to work. I may need to change my variable declaration and my public
accessor.

Thanks

How can I pull this information out of a datareader and assign it to variables

Morten Wennevik

unread,
Sep 1, 2004, 8:13:47 AM9/1/04
to
Hi Marty,

DataReader.GetSqlString() returns an SqlString. To get the string value from that SqlString use:

this.name = dtrMyReader.GetSqlString(1).Value;

You may need (dtrMyReader.GetSqlString()).Value

--
Happy Coding!
Morten Wennevik [C# MVP]

Zürcher See

unread,
Sep 1, 2004, 8:15:26 AM9/1/04
to
Sorry I didn't read all you post, but from your code seem that you become
only one string from the DB query or I'm wrong?

For that case there is specific method in the SqlCommand

object reply=SqlCommand.ExecuteScalar();
if (reply!=System.DBNull.Value) //ok is a string and not null

Don't forget to open and close the connection

"Marty U." <Marty U.@discussions.microsoft.com> schrieb im Newsbeitrag
news:3E325B79-7E3A-4EDA...@microsoft.com...

U.@discussions.microsoft.com Marty U.

unread,
Sep 1, 2004, 8:27:02 AM9/1/04
to
Actually the final query will have about 3 columns returned from the DB. I
tried the .Value and .value but neither worked. I still get Specified Cast is
invalid for the .Value and for the .value it states that "SqlTypes.SqlString"
does not contain a definition for 'value'.

I am stumped with this one. All I want to do is get a record from the
database based on a input parameter to the stored procedure and then set 3
public accessors to the values so I can reference them from the page.

"Zürcher See" wrote:

> ..Net

Russ Gray

unread,
Sep 1, 2004, 8:46:15 AM9/1/04
to
Marty U. wrote:
> Actually the final query will have about 3 columns returned from the DB. I
> tried the .Value and .value but neither worked. I still get Specified Cast is
> invalid for the .Value and for the .value it states that "SqlTypes.SqlString"
> does not contain a definition for 'value'.
>
> I am stumped with this one. All I want to do is get a record from the
> database based on a input parameter to the stored procedure and then set 3
> public accessors to the values so I can reference them from the page.

Can you not just use GetString instead of GetSqlString?

SqlDataReader dtrMyReader;
dtrMyReader = myCommand.ExecuteReader();
while (dtrMyReader.Read())
{

this.Name = dtrMyReader.GetString(1);
}

GetString is just a wrapper around GetSqlString anyway, but it saves
mucking around doing the conversion explicitly.

--
Remove SHOES to reply.

U.@discussions.microsoft.com Marty U.

unread,
Sep 1, 2004, 8:45:05 AM9/1/04
to
One more thing, the type returned for that column is System.String if this
effects how I do this.

U.@discussions.microsoft.com Marty U.

unread,
Sep 1, 2004, 8:53:04 AM9/1/04
to
I just tried that and I get the same error, Specified Cast Not Valid.

The things I have tried:

this.name = dtrGet.GetSqlString(1);
this.name = dtrGet.GetString(1);
this.name = dtrGet.GetSqlString(1).Value;
this.name = dtrGet.GetSqlString(1).value;
this.name = (dtrGet.GetSqlString(1)).value;
this.name = (dtrGet.GetSqlString(1)).Value;
this.name = dtrGet["cName"];
this.name = dtrGet["cName"].ToString();
this.name = dtrGet.GetString(1).value;
this.name = dtrGet.GetString(1).Value;

None of these worked and either gave me Specified Cast not Valid or it would
not compile at all. I have also tried doing conversions like Convert.ToString
but this also failed.

Ignacio Machin ( .NET/ C# MVP )

unread,
Sep 1, 2004, 9:03:42 AM9/1/04
to
Hi,

GetSqlString return a SqlString variable, not a string.

what you can do is call GetSqlString(1).ToString() it will convert it to
System.String

You could also do this ( I always use this method )

this.name = reader["TheFieldName"].ToString()

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Marty U." <Marty U.@discussions.microsoft.com> wrote in message
news:3E325B79-7E3A-4EDA...@microsoft.com...

Morten Wennevik

unread,
Sep 1, 2004, 9:08:31 AM9/1/04
to
Marty,

if you do

object o = dtrGet.Gestring(1);
MessageBox.Show(o.GetType().ToString());

What do you get?

U.@discussions.microsoft.com Marty U.

unread,
Sep 1, 2004, 9:19:07 AM9/1/04
to
I have got it and don't know why this did not work yesterday. Oh by the way I
am building a ASP.Net app but I have been forcing myself to learn OOP better
and focus on good application design. I don't use Visual Studio at the moment
so most of this is hand coded with the help of a couple free tools.

The solution which I did try yesterday but must have had something else
changed is:

this.name = Convert.ToString(dtrGet["cName"]);
I did this yesterday but it did not work so I must have had something else
different.

And by the way, what is the difference between object.ToString() vs
Convert.ToString(object);

Thanks for your help.

> ..Net

0 new messages