VJain
VJain

Reputation: 1059

How to return a string value in a method?

I have made a function like this

public int InsertData(CategoryPhoto catphoto)
{
    string ans = null;
    SqlCommand cmd = DataConnection.GetConnection().CreateCommand();
    cmd.CommandText = "prcCategoryPhoto";
    cmd.CommandType = CommandType.StoredProcedure;
    // cmd.Parameters.Add(new SqlParameter("@PhotoID", prdctphoto.PhotoID));
    cmd.Parameters.Add(new SqlParameter("@PhotoName", catphoto.PhotoName));
    //cmd.Parameters.Add(new SqlParameter("@LeftPhoto", prdctphoto.LeftPhoto));
    //cmd.Parameters.Add(new SqlParameter("@RightPhoto", prdctphoto.RightPhoto));
    //cmd.Parameters.Add(new SqlParameter("@BackPhoto", prdctphoto.BackPhoto));
    //cmd.Parameters.Add(new SqlParameter("@MaterialPhoto", prdctphoto.MaterialPhoto));
    cmd.Parameters.Add(new SqlParameter("@ExtName", catphoto.ExtName));
    cmd.Parameters.Add(new SqlParameter("@PhotoType", catphoto.PhotoType));
    cmd.Parameters.Add(new SqlParameter("@PhotoSize", catphoto.PhotoSize));
    cmd.Parameters.Add(new SqlParameter("@CategoryID", catphoto.CategoryID));
    ans = cmd.ExecuteScalar().ToString();
    //var result = cmd.ExecuteScalar(); 
    //ans = int.Parse(result.ToString());
    cmd.Dispose();
    DataConnection.CloseConnection();
    return ans;
}

In my stored procedure is

create proc [dbo].[prcCategoryPhoto]
(
@PhotoName  Varchar(100),
@ExtName    Varchar(100),
@PhotoType  Varchar(100),
@PhotoSize  int,
@CategoryID varchar(20)
)
as
insert into CategoryPhoto(PhotoName,ExtName,PhotoType,PhotoSize,CategoryID) 
    values (@PhotoName,@ExtName,@PhotoType,@PhotoSize,@CategoryID)
select @@IDENTITY

on writing return ans it is giving a error can not implicitly convert string to int

and on writing

return int.Parse(ans);

it gives exception that nvarchar cannot be converted to int

Upvotes: 0

Views: 192

Answers (6)

RobertKing
RobertKing

Reputation: 1921

try below, first alter your procedure, add a output parameter as below,

create proc [dbo].[prcCategoryPhoto]
(
@PhotoName  Varchar(100),
@ExtName    Varchar(100),
@PhotoType  Varchar(100),
@PhotoSize  int,
@CategoryID varchar(20),
@ID INT OUTPUT
)
as
insert into CategoryPhoto(PhotoName,ExtName,PhotoType,PhotoSize,CategoryID) 
    values (@PhotoName,@ExtName,@PhotoType,@PhotoSize,@CategoryID)
select @ID = @@IDENTITY

Next modify your function as below,

public int InsertData(CategoryPhoto catphoto)
{
    SqlCommand cmd = DataConnection.GetConnection().CreateCommand();
    cmd.CommandText = "prcCategoryPhoto";
    cmd.CommandType = CommandType.StoredProcedure;
    // cmd.Parameters.Add(new SqlParameter("@PhotoID", prdctphoto.PhotoID));
    cmd.Parameters.Add(new SqlParameter("@PhotoName", catphoto.PhotoName));
    //cmd.Parameters.Add(new SqlParameter("@LeftPhoto", prdctphoto.LeftPhoto));
    //cmd.Parameters.Add(new SqlParameter("@RightPhoto", prdctphoto.RightPhoto));
    //cmd.Parameters.Add(new SqlParameter("@BackPhoto", prdctphoto.BackPhoto));
    //cmd.Parameters.Add(new SqlParameter("@MaterialPhoto", prdctphoto.MaterialPhoto));
    cmd.Parameters.Add(new SqlParameter("@ExtName", catphoto.ExtName));
    cmd.Parameters.Add(new SqlParameter("@PhotoType", catphoto.PhotoType));
    cmd.Parameters.Add(new SqlParameter("@PhotoSize", catphoto.PhotoSize));
    cmd.Parameters.Add(new SqlParameter("@CategoryID", catphoto.CategoryID));

    cmd.Parameters.Add(new SqlParameter("@ID",System.Data.SqlDbType.Int));
    cmd.Parameters["@ID"].Direction=System.Data.ParameterDirection.Output;

    cmd.ExecuteNonQuery();
    var ans = cmd.Parameters["@ID"].Value;
    cmd.Dispose();
    DataConnection.CloseConnection();
    return Convert.ToInt32(ans);
}

Upvotes: 1

Ramesh Rajendran
Ramesh Rajendran

Reputation: 38683

The ScoapIdentity will return your primarykey value , So that datatype is int , So you need to declare to int variable(not a string variable) .

So Replace this line string ans = null; to int ans = null;

and also need to change this below line

ans = Convert.ToInt32(cmd.ExecuteScalar());

Upvotes: 0

Sasidharan
Sasidharan

Reputation: 3740

Replace

 public int InsertData(CategoryPhoto catphoto)

with

 public string InsertData(CategoryPhoto catphoto)

and follow the same for its dependancies...

Upvotes: 0

alzaimar
alzaimar

Reputation: 4622

You should be using the using keyword. It makes sure that the dispose method is called even if something goes wrong (i.e. and exception is raised).

Also, the @@Identity returns a numeric value in any case (according to MSDN), which should be convertible to an integer or a bigint. So my suggestion would be:

public Int64 InsertData(CategoryPhoto catphoto)
{
  using (var connection = DataConnection.GetConnection)
  {
    using (var cmd = connection.CreateCommand())
    {
      cmd.CommandText = "prcCategoryPhoto";
      cmd.CommandType = CommandType.StoredProcedure;
      cmd.Parameters.Add(new SqlParameter("@PhotoName", catphoto.PhotoName));
      cmd.Parameters.Add(new SqlParameter("@ExtName", catphoto.ExtName));
      cmd.Parameters.Add(new SqlParameter("@PhotoType", catphoto.PhotoType));
      cmd.Parameters.Add(new SqlParameter("@PhotoSize", catphoto.PhotoSize));
      cmd.Parameters.Add(new SqlParameter("@CategoryID", catphoto.CategoryID));
      return (Int64)cmd.ExecuteScalar();
    }
  }
}

In case your identity-column is an integer, you may of course change the signature of the method to return an int instead.

Upvotes: 0

The Hungry Dictator
The Hungry Dictator

Reputation: 3484

now try this.... `

   public string InsertData(CategoryPhoto catphoto)
   {

    string ans = null;
    SqlCommand cmd = DataConnection.GetConnection().CreateCommand();
    cmd.CommandText = "prcCategoryPhoto";
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add(new SqlParameter("@PhotoName", catphoto.PhotoName));
    cmd.Parameters.Add(new SqlParameter("@ExtName", catphoto.ExtName));
    cmd.Parameters.Add(new SqlParameter("@PhotoType", catphoto.PhotoType));
    cmd.Parameters.Add(new SqlParameter("@PhotoSize", catphoto.PhotoSize));
    cmd.Parameters.Add(new SqlParameter("@CategoryID", catphoto.CategoryID));
    ans = cmd.ExecuteScalar().ToString();
    cmd.Dispose();
    DataConnection.CloseConnection();
    return ans;
  }`

Upvotes: 1

Anarion
Anarion

Reputation: 2534

Look at your string (ans) in debugger. Seems like you have some characters that can not be converted to int. Can you change the return type of the method from int to string?

Upvotes: 0

Related Questions