English 中文(简体)
ASP. 互联网标签正在显示“系统.Data”信息
原标题:ASP.NET label is displaying "System.Data" message

我在试图从一个数据库中显示数据时,在纸浆上展示了奇怪的信息。 在网页上:alabel指从一个数据来源中的人口,但显示以下信息/文本。 System.Data.SqlClient.SqlDataReader"

怎样才能做到这一点?

I have written a small method in the page load of the .aspx.cs page. labelName is the one which is displaying this message:

public partial class edit_questionnaire : System.Web.UI.Page
    {
        OsqarSQL GetData;

        protected void Page_Load(object sender, EventArgs e)
        {
            string questionnaireId = Session["qID"].ToString();
            int qid = Convert.ToInt32(questionnaireId);
            GetData = new OsqarSQL();
            string name = GetData.GetQuestionnaireName(qid);
            labelName.Text = name;

        }
    }

采用以下方法:

public string GetQuestionnaireName(int questionnaireId)
        {
            string returnValue = string.Empty;
            SqlCommand myCommand = new SqlCommand("GetQuestionnaireName", _productConn);
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.Add(new SqlParameter("@QUEST_ID", SqlDbType.Int));
            myCommand.Parameters[0].Value = questionnaireId;
            SqlDataReader qName = getData(myCommand);
            while (qName.Read())
            {
               returnValue = qName.ToString();
            }
            _productConn.Close();
            return returnValue;
        }

并且使用这一储存程序:

ALTER PROCEDURE [hgomez].[GetQuestionnaireName] 
    (
    @QUEST_ID int
    )   
AS
    /*SET NOCOUNT ON;*/
    SELECT QuestionnaireName FROM [Questionnaires] WHERE QuestionnaireID = @QUEST_ID
    RETURN
最佳回答
public string GetQuestionnaireName(int questionnaireId)
    {
        string returnValue = string.Empty;
        SqlCommand myCommand = new SqlCommand("GetQuestionnaireName", _productConn);
        myCommand.CommandType = CommandType.StoredProcedure;
        myCommand.Parameters.Add(new SqlParameter("@QUEST_ID", SqlDbType.Int));
        myCommand.Parameters[0].Value = questionnaireId;
        SqlDataReader qName = getData(myCommand);
        while (qName.Read())
        {
           returnValue = qName[0].ToString();
        }
        _productConn.Close();
        return returnValue;
    }

你将SqlDataReader派回来。 价值而不是阅读价值。

问题回答

您需要使用<代码>GetValue方法>

SqlDataReader qName = getData(myCommand);
while (qName.Read())
{
   returnValue = qName.GetValue(0).ToString();
}

问题如下:它是数据读物,而不是数据读物所退回的插图:

returnValue = qName.ToString();

You should replace this with

returnValue = qName.GetString(0);

你们正在做的是<代码>。 缩略语 改为<编码>。 SqlDataReader 页: 1 作为参数,你应当通过实地指数。 你只想在这里读到一个零的领域。

你们应该回过头来。

returnValue = qName.GetString(0);

或者说,你可以这样做

returnValue = qName.GetString("QuestionnaireName");
//this is better to name fields

或者说,你可以简单写一下

returnValue = qName[0].ToString();

That s because you are calling SqlDataReader.ToString(). That will return the string of the type, which is in fact "System.Data.SqlClient.SqlDataReader".

使用<代码> 数值=qName.GetString(0);

You are converting qName.ToString(), you can try qName[0].ToString(); with [0] being the index of select column inside your stored procedure.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签