0

I am trying to get value from the databse and assign it to the label using the below line:

lblQuestion.Text = ds.Tables[0].Rows[0]["Question"].ToString(); 

but it assigns as label.

          if (ds.Tables[0].Rows.Count > 0)
            {
                lblQuestion.Text = ds.Tables[0].Rows[0]["Question"].ToString(); ;
            }
1
  • 6
    Not sure what you mean with "but it assigns as label". Could you clarify that? Commented Aug 20, 2010 at 17:34

3 Answers 3

1

UPDATED - Deleted my last info now that you have included the code....

Have you stepped through your code to see if any data is returning?

AKA - is ds.Tables[0].Rows.Count > 0 ?

You are also doing !Page.IsPostBack. This will only call the code and load your labels if it's on the first load.... What about subsequent loads? The labels will return back to whatever they defaulted to in the designer.....

Sign up to request clarification or add additional context in comments.

1 Comment

ToString() doesn't return the ColumnName property of the column as .Rows[0]["Question"] uses indexers to get the value of the Question column for the first record in the table.
1

I've put together a test-case in a web app as follows:

ASPX Code-behind:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var ds = new DataSet();
        var table = new DataTable();
        table.Columns.Add("Question", typeof(string));
        table.Rows.Add(new object[] { "This is the question" });

        ds.Tables.Add(table);

        lblQuestion.Text = ds.Tables[0].Rows[0]["Question"].ToString();
    }
}

ASPX Page:

<form id="form1" runat="server">
<div>
    <asp:Label runat="server" ID="lblQuestion"></asp:Label>
</div>
</form>

This renders out correctly as:

<div>
    <span id="lblQuestion">This is the question</span>
</div>

Are you sure you've shown us the code that's actually running?

Comments

0
protected void Page_Load(object sender, EventArgs e)
    {
        //if (Request.QueryString["QID"] != null)
        //{
        //    Response.Write(Request.QueryString["QID"].ToString()); 
        //}
        if (!Page.IsPostBack)
        {
            int i = Convert.ToInt32(Request.QueryString["QID"]);
            // Response.Write(i);
            eTutorService ServiceProvider = new eTutorService();
            DataSet ds = new DataSet();
            ds = ServiceProvider.GetQuestionView(i);
            if (ds.Tables[0].Rows.Count > 0)
            {
                lblQuestion.Text = ds.Tables[0].Rows[0]["Question"].ToString(); ;
                lblOption1.Text = ds.Tables[0].Rows[0]["Option1"].ToString();
                lblOption2.Text = ds.Tables[0].Rows[0]["Option2"].ToString();
                lblOption3.Text = ds.Tables[0].Rows[0]["Option3"].ToString();
                lblOption4.Text = ds.Tables[0].Rows[0]["Option4"].ToString();
                lblCorrectOption.Text = ds.Tables[0].Rows[0]["CorrectAnswer"].ToString();
                lblPaper.Text = ds.Tables[0].Rows[0]["subject"].ToString();
                lblDifficultyLevel.Text = ds.Tables[0].Rows[0]["LEVEL_NAME"].ToString();
                lblquestionOrder.Text = ds.Tables[0].Rows[0]["QuestionOrder"].ToString();
            }
        }


      }

Its just put label rather than putting database value to the label

1 Comment

Next time just put the code in your initial question instead of adding as an answer.... :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.