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
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]
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...
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
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.
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.
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...
if you do
object o = dtrGet.Gestring(1);
MessageBox.Show(o.GetType().ToString());
What do you get?
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