user6592730 user6592730 - 4 months ago 27
ASP.NET (C#) Question

How to return string value through a sql query?

I have a query which retuns a string value as the output. But shows an error in the return. I dont know how to fix it. Here i have added my query.

public string detailsRemarksGet(string ddlValue)

string strQuery = @"select r.remarks
from [A_MASTER] m, [A_REMARKS] r
where m.A_REF_NO=r.A_REF_NO
and r.A_REF_NO='"+ ddlValue +"' and DEPT='POS' ";
return SqlHelper.ExecuteScalar(strConnStringAppeal, CommandType.Text, strQuery);

Here is mky code of .cs

public string detailsRemarks(string ddlValue)
string remarks= db.detailsRemarksGet(ddlValue);
return remarks;

In the error list i get a message as follows

Error 2 Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)


Sql query return object type, you need to cast that as string. So cast as string using ToString() method in return statement

return SqlHelper.ExecuteScalar(strConnStringAppeal, CommandType.Text, strQuery).ToString();