TechGuy
TechGuy

Reputation: 4560

Cannot implicitly convert type 'System.Data.DataSet' to 'string' access Div in Server Side

I'm going to access my Div in Server side then I need to bind data to that div

My Code:

 <div id="leftpanel" class="panel-body" runat="server">
                </div>

Server Side code:

public void GetLeftPanelData()
{
    DataAccess da = new DataAccess();
    leftpanel.InnerText = da.GetLeftPanelData(); // <-- Error Shown here
}

Data Access Method

public DataSet GetLeftPanelData()
{
    try
    {
        SqlCommand comGetLeftPanelData = new SqlCommand("SP_GetLeftPanelData", con);
        comGetLeftPanelData.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(comGetLeftPanelData);
        DataSet ds = new DataSet();
        con.Open();
        da.Fill(ds);
        con.Close();
        return ds;
    }
    catch (Exception ee)
    {
        throw ee;
    }
}

My Stored Procedure

ALTER PROCEDURE [dbo].[SP_GetLeftPanelData]

AS
BEGIN
select Description 
from dbo.CommonPages
where Type= 6
END

Here is returning encoded HTML ![enter image description here][1]

In this query just returns 1 line of strings Like "Hello test data"

Upvotes: 0

Views: 5304

Answers (2)

rhughes
rhughes

Reputation: 9583

If you must use this method (I advise not to), change your method to this:

public string GetLeftPanelData()
{
    try
    {
        SqlCommand comGetLeftPanelData = new SqlCommand("SP_GetLeftPanelData", con);
        comGetLeftPanelData.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(comGetLeftPanelData);
        DataSet ds = new DataSet();
        con.Open();
        da.Fill(ds);
        con.Close();
        return ds.Tables[0].Rows[0]["Description"].ToString();
    }
    catch (Exception ee)
    {
        throw ee;
    }
}

Upvotes: 1

BRW
BRW

Reputation: 353

Assuming that what I'm seeing in your updated question is correct, I would suggest using ExecuteScalar().

This is used when you have a query that only returns a single piece of data. In this case, it looks like your query only returns a single row, with only the column Description. If that is correct, you could then do something like this:

public string GetLeftPanelData()
{
    try
    {
        SqlCommand comGetLeftPanelData = new SqlCommand("SP_GetLeftPanelData", con);
        comGetLeftPanelData.CommandType = CommandType.StoredProcedure;
        con.Open();
        string returnData = (string)comGetLeftPanelData.ExecuteScalar();
        con.Close();
        return returnData;
    }
    catch (Exception ee)
    {
        throw ee;
    }
}

Upvotes: 2

Related Questions