Getting SQL Data to display in webforms

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BezerkRogue
    New Member
    • Oct 2007
    • 68

    #1

    Getting SQL Data to display in webforms

    I have constructed a query and want the data to load in specific locations in the page when the page loads. I keep getting BC30311: Value of type 'String' cannot be converted to 'System.Web.UI. WebControls.Tex tBox'. when I run the page.

    Here is the code I have so far:

    Dim strSQL
    Dim strShift
    Dim strSelDate

    strShift = lblShift.Text
    strSelDate = lblSelDate.Text
    strSQL = "SELECT * FROM Manpower_data INNER JOIN Shift ON Shift.Shift_ID = Manpower_data.S hift_ID WHERE Manpower_data.D ate = '" & strSelDate & "' AND Shift.Shift = '" & strShift & "'"
    Dim objConn As New Data.SqlClient. SqlConnection(" Data Source=SomeServ er;Initial Catalog=SomeDB; User ID=SomeUser;pas sword=XXXXXXX;" )
    Dim objCmd As New Data.SqlClient. SqlDataAdapter( strSQL, objConn)
    Dim objDataSet As New Data.DataSet
    objCmd.Fill(obj DataSet)
    objConn.Open()
    objConn.Close()

    txtAbsent = objDataSet.Tabl es("Absent").To String
    txtTO = objDataSet.Tabl es("TO").ToStri ng
    txtHeadCount = objDataSet.Tabl es("HeadCount") .ToString
    txtStdHeadCount = objDataSet.Tabl es("STDHeadCoun t").ToString
    txtMPNotes = objDataSet.Tabl es("Notes").ToS tring

    What am I missing?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    The error message should have been the big hint.

    You have:
    Code:
    txtAbsent = objDataSet.Tables("Absent").ToString
    txtTO = objDataSet.Tables("TO").ToString
    txtHeadCount = objDataSet.Tables("HeadCount").ToString
    txtStdHeadCount = objDataSet.Tables("STDHeadCount").ToString
    txtMPNotes = objDataSet.Tables("Notes").ToString
    What you need is:
    Code:
    txtAbsent.Text = objDataSet.Tables("Absent").ToString
    txtTO.Text = objDataSet.Tables("TO").ToString
    txtHeadCount.Text = objDataSet.Tables("HeadCount").ToString
    txtStdHeadCount.Text = objDataSet.Tables("STDHeadCount").ToString
    txtMPNotes.Text = objDataSet.Tables("Notes").ToString

    However, the object you are trying to fill them with, will have a .ToString() of something like "DataTable" and not your values, you will need to pick the value from out of the table.

    Comment

    • BezerkRogue
      New Member
      • Oct 2007
      • 68

      #3
      Doh.... thanks man. ur a lifesaver.

      Comment

      • BezerkRogue
        New Member
        • Oct 2007
        • 68

        #4
        Originally posted by Plater
        The error message should have been the big hint.

        You have:
        Code:
        txtAbsent = objDataSet.Tables("Absent").ToString
        txtTO = objDataSet.Tables("TO").ToString
        txtHeadCount = objDataSet.Tables("HeadCount").ToString
        txtStdHeadCount = objDataSet.Tables("STDHeadCount").ToString
        txtMPNotes = objDataSet.Tables("Notes").ToString
        What you need is:
        Code:
        txtAbsent.Text = objDataSet.Tables("Absent").ToString
        txtTO.Text = objDataSet.Tables("TO").ToString
        txtHeadCount.Text = objDataSet.Tables("HeadCount").ToString
        txtStdHeadCount.Text = objDataSet.Tables("STDHeadCount").ToString
        txtMPNotes.Text = objDataSet.Tables("Notes").ToString

        However, the object you are trying to fill them with, will have a .ToString() of something like "DataTable" and not your values, you will need to pick the value from out of the table.
        How do I do this with this code? I get the expected Object reference not set to an instance of an object error message.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          This line:
          Code:
          objDataSet.Tables("Absent")
          Is looking for a table called "Absent" in your dataset. I am going to guess you want a column by that name?

          You will need to get at your table, there is probably only one table:
          Code:
          DataTable dt=objDataSet.Tables[0];
          And then you will need the row, is there only going to be one row?
          Code:
          DataRow dr=dt.Rows[0];
          Then from THAT you can get your columns
          Code:
          dr["Absent"].ToString()

          You should be able to condense down if you want:
          Code:
          objDataSet.Tables[0].Rows[0]["Absent"].ToString();

          Comment

          • BezerkRogue
            New Member
            • Oct 2007
            • 68

            #6
            Originally posted by Plater
            This line:
            Code:
            objDataSet.Tables("Absent")
            Is looking for a table called "Absent" in your dataset. I am going to guess you want a column by that name?

            You will need to get at your table, there is probably only one table:
            Code:
            DataTable dt=objDataSet.Tables[0];
            And then you will need the row, is there only going to be one row?
            Code:
            DataRow dr=dt.Rows[0];
            Then from THAT you can get your columns
            Code:
            dr["Absent"].ToString()

            You should be able to condense down if you want:
            Code:
            objDataSet.Tables[0].Rows[0]["Absent"].ToString();
            Thanks. That's working great.

            Comment

            Working...