Reputation: 1534
I am having a strange message displayed on the asp:label when trying to display data from a database. During page_load the asp:label is meant to be populated from a datasource however is displays the following message/text "System.Data.SqlClient.SqlDataReader"
What could be causing this?
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;
}
}
Which calls the following method:
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;
}
And uses this stored procedure:
ALTER PROCEDURE [hgomez].[GetQuestionnaireName]
(
@QUEST_ID int
)
AS
/*SET NOCOUNT ON;*/
SELECT QuestionnaireName FROM [Questionnaires] WHERE QuestionnaireID = @QUEST_ID
RETURN
Upvotes: 2
Views: 946
Reputation: 13018
You are converting qName.ToString()
, you can try qName[0].ToString();
with [0] being the index of select column inside your stored procedure.
Upvotes: 0
Reputation: 5605
What you are getting is SqlDataReader
type as string. To read a string out of SqlDataReader
you will have to use GetString
method. And as parameter you should pass the index of field. As you have only one field which you are trying to read pass zero here.
You should return value like this
returnValue = qName.GetString(0);
Alternatively you can do
returnValue = qName.GetString("QuestionnaireName");
//this is better to name fields
Or you can simply write
returnValue = qName[0].ToString();
Upvotes: 1
Reputation:
That's because you are calling SqlDataReader.ToString()
. That will return the string of the type, which is in fact "System.Data.SqlClient.SqlDataReader"
.
Use returnValue = qName.GetString(0);
Upvotes: 0
Reputation: 7600
You need to use the GetValue
method of SqlDataReader
SqlDataReader qName = getData(myCommand);
while (qName.Read())
{
returnValue = qName.GetValue(0).ToString();
}
Upvotes: 2
Reputation: 660
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;
}
You were assigning the SqlDataReader to your returnValue rather than reading the value.
Upvotes: 3
Reputation: 726489
The problem is in the line below - it is a data reader, not a string returned by the data reader:
returnValue = qName.ToString();
You should replace this with
returnValue = qName.GetString(0);
Upvotes: 1