0

I have been trying for hours to locate a bug in my code, and its killing me, that i can't find it.

When running Visual Studio in Debug mode I can see that the DataTable is just empty. I have testet the Sql, which finds the data.

Below is the debug-screen, program and DB-table.

I hope someone can put me in the right direction.

Debug screenshot

Debugscreen

The program

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DebugTestPage : System.Web.UI.Page
{
    public string QsId;
    public string QsModel;

    protected void Page_Load(object sender, EventArgs e)
    {
        //Til Debugging
        QsModel = "Category";
        QsId = "2";

        //QsModel = "Thread";
        //QsId = "1";

        ////Til debugging
        //Response.Write(GetEditDataFromDb(QsModel, QsId));

        DataTable NewDT = GetEditDataFromDb(QsModel, QsId);

        Response.Write(NewDT);
    }

    public static DataTable GetEditDataFromDb(string QsModel, string QsId)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
        SqlCommand cmd = new SqlCommand();

        cmd.CommandType = CommandType.StoredProcedure;



        switch (QsModel)
        {

            case "Category":
                cmd.CommandText = "GetCategoryTableDataSP";
                break;
            case "Thread":
                cmd.CommandText = "GetThreadAndFirstPostSP";
                break;
            //case "Post":
            //    cmd.CommandText = "GetPostEditDataSP";
            //    break;
            //case "User":
            //    cmd.CommandText = "GetUserEditDataSP";
            //    break;

        }

        cmd.Parameters.Add("@Id", SqlDbType.Int).Value = Convert.ToInt32(QsId);

        cmd.Connection = conn;

        conn.Open();

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        conn.Close();
        return dt;
    }

Stored Procudere

//    CREATE PROCEDURE [dbo].[GetCategoryTableDataSP]
//    (
//    @Id int
//    )
//AS

//BEGIN

//SELECT * FROM Category WHERE Category.Id = @Id

//END

DB-table

Database table

UPDATE: I have tried binding the datatable to a gridview instead of Response.Write() it on the page, which writes the data on the page. I guess its not possible to just use Response.Write(DataTable), so it seems like i found the bug myself. I will leave the question open, in case someone has a better explanation.

I also don't understand, why the debugging shows the datatable "dt" value (see debug screenshot above) as empty. according to this, it should show the datatable.

Community
  • 1
  • 1
chmodder
  • 184
  • 1
  • 3
  • 17

0 Answers0