Re: Specified Cast is not Valid

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Marty U. (U._at_discussions.microsoft.com)
Date: 09/01/04


Date: Wed, 1 Sep 2004 05:27:02 -0700

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:

> 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-BB12-D1256DBE5FE2@microsoft.com...
> > 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
>
>
>



Relevant Pages

  • Re: CONTAINS performance
    ... That said, and with the query plan, I can start to give you more ... relational join in the context of the free-text optimization, ... SQL Server tables. ...
    (microsoft.public.sqlserver.fulltext)
  • Re: Access to SQLServer GCE
    ... Actually, as Larry points out, the Access client does a good job of filtering only information that you need. ... If your query is bound to a report, and you open that report supplying a typical "where" for that report, then Access will NOT pull down all the records, but in most cases Access will only pull down the required records and respect your filter. ... So SQL server will respect the conditions and filtering placed into those queries, and therefore only pull down those records you require. ... The suggestion in these cases is to consider using a pass-through query since all of that summing is done before the row comes down the network pipe. ...
    (comp.databases.ms-access)
  • Re: Indexing Service, Openquery and sp_executesql
    ... SQL Server version and sp are you running? ... > data from the indexing service catalog when pasted into ... > query analyzer, but failed when put against sp_executesql ... I would choose Microsoft Indexing ...
    (microsoft.public.sqlserver.fulltext)
  • Re: Problem using Access or Query Designer to run queries in SQL Serve
    ... >or Query Designer within Enterprise Manager, it works and I get data back. ... >ODBC Call Failed [ODBC SQL Server Driver] Timeout Expirederror in Access ... >[ODBC SQL Server Driver] Timeout Expired ...
    (microsoft.public.sqlserver.odbc)
  • Extreme performance issues (SQL Server 2000/ADO.NET/C#)
    ... This process runs very quickly if run through Query ... same exact stored procedures and views, run in the same exact order, through ... system that runs SQL Server (a 4-cpu Xeons system with 2gigs of physical ... When I execute these steps manually through query analyser,, ...
    (microsoft.public.dotnet.framework.adonet)