Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Apr 28, 2012 09:04 PM by AidyF
Member
4 Points
164 Posts
Apr 28, 2012 06:46 PM|LINK
I am getting this error "Can't implictly convert type object to string"?
i want to extract columns (procedureid, procedurefilename, procedurefilename) from the reader. i have to retirn a claass object.
public static Procedures GetProcedureByGSearch(string ProcedureFileName, stringProcedureLocation) { Procedures _Procedures = new Procedures(); using(SqlConnection sqlConnection = new SqlConnection(AppConfiguration.ConnectionString)) { string storedProcedureName = "GetProcedureByFileNameAndPath"; SqlCommand sqlCommand = new SqlCommand(); sqlCommand.CommandType = CommandType.StoredProcedure; sqlCommand.Connection = sqlConnection; sqlCommand.CommandText = storedProcedureName; Database.ParameterManager.AddParameterToCommand(sqlCommand, "@ProcedureFileName", ProcedureFileName); Database.ParameterManager.AddParameterToCommand(sqlCommand, "@ProcedureLocation", ProcedureLocation); sqlConnection.Open(); using(SqlDataReader sqlDataReader = sqlCommand.ExecuteReader()) { if (sqlDataReader.Read()) { _Procedures.ProcedureID = sqlDataReader[ProcedureID]; ----------------------------------Error _Procedures.ProcedureFileName = sqlDataReader[ProcedureFileName]; -----------------error _Procedures.ProcedureLocation = sqlDataReader[ProcedureLocation]; --------------------Error } sqlDataReader.Close(); } sqlConnection.Close(); } return _Procedures; }
431 Points
369 Posts
Apr 28, 2012 07:01 PM|LINK
Please change in your if condition as below :
if (sqlDataReader.Read())
{
_Procedures.ProcedureID = Convert.ToInt16(sqlDataReader[ProcedureID]);
_Procedures.ProcedureFileName = sqlDataReader[ProcedureFileName].toString(); //If data type if String
_Procedures.ProcedureLocation = sqlDataReader[ProcedureLocation].toString();
}
Apr 28, 2012 07:17 PM|LINK
this doesn't solve the problem
still getting error but different one.
Apr 28, 2012 07:29 PM|LINK
can you show us the problem / error ?
Star
9204 Points
1570 Posts
Apr 28, 2012 08:03 PM|LINK
I'll generalise your problems as we don't know the types you are dealing with. For this line
_Procedures.SomeProperty = sqlDataReader[index];
You have to cast the sqlDataReader[index] ro whatever type SomeProperty is. If SomeProperty is an int
_Procedures.SomeProperty = (int) sqlDataReader[index];
If SomeProperty is a string
_Procedures.SomeProperty = (string) sqlDataReader[index];
If it is a double
_Procedures.SomeProperty = (double) sqlDataReader[index];
And so on.
301 Points
67 Posts
Apr 28, 2012 08:24 PM|LINK
int myint = (int) result.fromDatabase;
has the effect of casting the datbase object returned into an int data type.
Apr 28, 2012 08:46 PM|LINK
Hi Guys the Error is "theError 2 The name 'ProcedureID' does not exist in the current context "
I am getting two values as parameters and on the basis of these values i have to get data from Db i.e three columns.
(Parameters = 2, output columns from DB=3)
public static Procedures GetProcedureByGSearch(string ProcedureFileName, string ProcedureLocation)
Procedures _Procedures = new Procedures();
using (SqlConnection sqlConnection = new SqlConnection(AppConfiguration.ConnectionString))
string storedProcedureName = "ProcedureByFileNameAndPath";
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.CommandType =
CommandType.StoredProcedure;
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = storedProcedureName;
Database.ParameterManager.AddParameterToCommand(sqlCommand,
"@ProcedureFileName", ProcedureFileName);
"@ProcedureLocation", ProcedureLocation);
sqlConnection.Open();
using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
//_Procedures = FillDataRecord(sqlDataReader);
_Procedures.ProcedureID = (
int)sqlDataReader[ProcedureID];-------------------------------------ERROR
_Procedures.ProcedureFileName = (
String)sqlDataReader[ProcedureFileName];
_Procedures.ProcedureLocation = (
string)sqlDataReader[ProcedureLocation];
sqlDataReader.Close();
sqlConnection.Close();
return _Procedures;
Apr 28, 2012 08:48 PM|LINK
MY Store Procedure
USE
[Infosource]
GO
if
exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ProcedureByFileNameAndPath]')
and
OBJECTPROPERTY(id, N'IsProcedure') = 1
)
drop
procedure [dbo].[ProcedureByFileNameAndPath]
SET
ANSI_NULLS
ON
QUOTED_IDENTIFIER
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
CREATE
PROCEDURE [dbo].[ProcedureByFileNameAndPath]
@ProcedureFileName
varchar(100)
,@ProcedureLocation varchar(200)
AS
BEGIN
SET NOCOUNT ON;
SELECT ProcedureID, ProcedureLocation, ProcedureFileName
FROM infosource.dbo.Procedures p
JOIN infosource.dbo.ProcedureType pt ON pt.ProcedureTypeID = p.ProcedureTypeID
WHERE Procedurelocation = @ProcedureLocation AND ProcedureFileName = @ProcedureFileName
End
GRANT
EXECUTE ON [dbo].[ProcedureByFileNameAndPath] TO [ASPINFOSOURCE]
Apr 28, 2012 09:04 PM|LINK
The number in the square brackets is the index of the field you want back. As ProcedureID is the first item in your select, that is index 0, and so on.
_Procedures.ProcedureID = (int)sqlDataReader[0]; _Procedures.ProcedureFileName = (String)sqlDataReader[1]; _Procedures.ProcedureLocation = (string)sqlDataReader[2];
Or you can set up variables or constants for these values
const int ProcedureID = 0; const int ProcedureFileName = 1; const int ProcedureLocation = 2; _Procedures.ProcedureID = (int)sqlDataReader[ProcedureID]; _Procedures.ProcedureFileName = (String)sqlDataReader[ProcedureFileName]; _Procedures.ProcedureLocation = (string)sqlDataReader[ProcedureLocation];
muhammadazee...
Member
4 Points
164 Posts
is this code valid?
Apr 28, 2012 06:46 PM|LINK
I am getting this error "Can't implictly convert type object to string"?
i want to extract columns (procedureid, procedurefilename, procedurefilename) from the reader. i have to retirn a claass object.
public static Procedures GetProcedureByGSearch(string ProcedureFileName, stringProcedureLocation) { Procedures _Procedures = new Procedures(); using(SqlConnection sqlConnection = new SqlConnection(AppConfiguration.ConnectionString)) { string storedProcedureName = "GetProcedureByFileNameAndPath"; SqlCommand sqlCommand = new SqlCommand(); sqlCommand.CommandType = CommandType.StoredProcedure; sqlCommand.Connection = sqlConnection; sqlCommand.CommandText = storedProcedureName; Database.ParameterManager.AddParameterToCommand(sqlCommand, "@ProcedureFileName", ProcedureFileName); Database.ParameterManager.AddParameterToCommand(sqlCommand, "@ProcedureLocation", ProcedureLocation); sqlConnection.Open(); using(SqlDataReader sqlDataReader = sqlCommand.ExecuteReader()) { if (sqlDataReader.Read()) { _Procedures.ProcedureID = sqlDataReader[ProcedureID]; ----------------------------------Error _Procedures.ProcedureFileName = sqlDataReader[ProcedureFileName]; -----------------error _Procedures.ProcedureLocation = sqlDataReader[ProcedureLocation]; --------------------Error } sqlDataReader.Close(); } sqlConnection.Close(); } return _Procedures; }Silverlight....
Member
431 Points
369 Posts
Re: is this code valid?
Apr 28, 2012 07:01 PM|LINK
Please change in your if condition as below :
if (sqlDataReader.Read())
{
_Procedures.ProcedureID = Convert.ToInt16(sqlDataReader[ProcedureID]);
_Procedures.ProcedureFileName = sqlDataReader[ProcedureFileName].toString(); //If data type if String
_Procedures.ProcedureLocation = sqlDataReader[ProcedureLocation].toString();
}
muhammadazee...
Member
4 Points
164 Posts
Re: is this code valid?
Apr 28, 2012 07:17 PM|LINK
this doesn't solve the problem
still getting error but different one.
Silverlight....
Member
431 Points
369 Posts
Re: is this code valid?
Apr 28, 2012 07:29 PM|LINK
can you show us the problem / error ?
AidyF
Star
9204 Points
1570 Posts
Re: is this code valid?
Apr 28, 2012 08:03 PM|LINK
I'll generalise your problems as we don't know the types you are dealing with. For this line
You have to cast the sqlDataReader[index] ro whatever type SomeProperty is. If SomeProperty is an int
If SomeProperty is a string
If it is a double
And so on.
madflatpicke...
Member
301 Points
67 Posts
Re: is this code valid?
Apr 28, 2012 08:24 PM|LINK
has the effect of casting the datbase object returned into an int data type.
muhammadazee...
Member
4 Points
164 Posts
Re: is this code valid?
Apr 28, 2012 08:46 PM|LINK
Hi Guys the Error is "theError 2 The name 'ProcedureID' does not exist in the current context "
I am getting two values as parameters and on the basis of these values i have to get data from Db i.e three columns.
(Parameters = 2, output columns from DB=3)
public static Procedures GetProcedureByGSearch(string ProcedureFileName, string ProcedureLocation)
{
Procedures _Procedures = new Procedures();
using (SqlConnection sqlConnection = new SqlConnection(AppConfiguration.ConnectionString))
{
string storedProcedureName = "ProcedureByFileNameAndPath";
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.CommandType =
CommandType.StoredProcedure;
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = storedProcedureName;
Database.ParameterManager.AddParameterToCommand(sqlCommand,
"@ProcedureFileName", ProcedureFileName);
Database.ParameterManager.AddParameterToCommand(sqlCommand,
"@ProcedureLocation", ProcedureLocation);
sqlConnection.Open();
using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
{
if (sqlDataReader.Read())
{
//_Procedures = FillDataRecord(sqlDataReader);
_Procedures.ProcedureID = (
int)sqlDataReader[ProcedureID];-------------------------------------ERROR
_Procedures.ProcedureFileName = (
String)sqlDataReader[ProcedureFileName];
_Procedures.ProcedureLocation = (
string)sqlDataReader[ProcedureLocation];
}
sqlDataReader.Close();
}
sqlConnection.Close();
}
return _Procedures;
}
muhammadazee...
Member
4 Points
164 Posts
Re: is this code valid?
Apr 28, 2012 08:48 PM|LINK
MY Store Procedure
USE
[Infosource]
GO
if
exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ProcedureByFileNameAndPath]')
and
OBJECTPROPERTY(id, N'IsProcedure') = 1
)
drop
procedure [dbo].[ProcedureByFileNameAndPath]
GO
SET
ANSI_NULLS
ON
GO
SET
QUOTED_IDENTIFIER
ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE
PROCEDURE [dbo].[ProcedureByFileNameAndPath]
@ProcedureFileName
varchar(100)
,@ProcedureLocation varchar(200)
AS
BEGIN
SET NOCOUNT ON;
SELECT ProcedureID, ProcedureLocation, ProcedureFileName
FROM infosource.dbo.Procedures p
JOIN infosource.dbo.ProcedureType pt ON pt.ProcedureTypeID = p.ProcedureTypeID
WHERE Procedurelocation = @ProcedureLocation AND ProcedureFileName = @ProcedureFileName
End
GO
GRANT
EXECUTE ON [dbo].[ProcedureByFileNameAndPath] TO [ASPINFOSOURCE]
GO
AidyF
Star
9204 Points
1570 Posts
Re: is this code valid?
Apr 28, 2012 09:04 PM|LINK
The number in the square brackets is the index of the field you want back. As ProcedureID is the first item in your select, that is index 0, and so on.
Or you can set up variables or constants for these values