0

Can somebody please explain why Clear() or Dispose() fail to flush a DataTable recordset?

I am using DataTable in an MVC 4 project like so var tbl = new DataTable() which is working fine for retrieving data, but for some reason when I use tbl.Clear() and tbl.Dispose() and then attempt to reuse tbl it still has the recordset from the previous query, why?

Can somebody please let me know how to flush/clear/empty the previous DataTable recordset?

Any help would be much appreciated :-)

Please see example code below:

string name = "";
string password = "";

var stringWriter = new StringWriter();
var writer = new XmlTextWriter(stringWriter) { Formatting = Formatting.Indented };    

var tbl = new DataTable();

string strConn = WebConfigurationManager.ConnectionStrings["TestConnection"].ToString();

var conn = new SqlConnection(strConn);
using (conn)
{
    conn.Open();
    var cmd = new SqlCommand("GetUserLoginID", conn) {CommandType = CommandType.StoredProcedure};
    cmd.Parameters.AddWithValue("@UID", uid);
    cmd.Parameters.AddWithValue("@BID", bId);

    var userRecord = new SqlDataAdapter(cmd);

    userRecord.Fill(tbl);
    conn.Close();

    if (tbl.Rows.Count > 0)
    {
        var row = tbl.Rows[0];

        name = row["Name"].ToString();
        password = row["Password"].ToString();
        code = row["Code"].ToString();
    }

    tbl.Clear();
    tbl.Dispose();

    cmd = new SqlCommand("GetUserDetails", conn) { CommandType = CommandType.StoredProcedure };
    cmd.Parameters.AddWithValue("@Code", code);
    cmd.Parameters.AddWithValue("@BID", bId);

    userRecord.Fill(tbl);

    if (tbl.Rows.Count > 0)
    {
        var row = tbl.Rows[0];

        writer.WriteStartDocument();
        writer.WriteStartElement("Root");
        writer.WriteElementString("BID", row["BID"].ToString());
        writer.WriteElementString("Name", name);

    }
    else
    {
        writer.WriteStartDocument();
        writer.WriteStartElement("Root");
        writer.WriteElementString("BID", "Error");
    }
    writer.WriteEndElement();
    writer.Flush();
    writer.Close();
}
2
  • Don't you get an exception when calling the Clear method? Can you you show us more code.
    – NicoD
    Jun 24, 2014 at 9:04
  • Please see my amended post.
    – iggyweb
    Jun 24, 2014 at 10:08

2 Answers 2

1

Seems you always use the same SqlDataAdapter. You created a new Command :

cmd = new SqlCommand("GetUserDetails", conn) { CommandType = CommandType.StoredProcedure };

but you have to set userRecord with this new Command:

userRecord = new SqlDataAdapter(cmd);
1
  • Yes I'm reusing SqlDataAdapter but using a different Stored Procedure, so doesn't that overwrite the previous connection. I've tried adding conn.Close() and conn.Open() before calling cmd = new SqlCommand("GetUserDetails", conn) but still no joy.
    – iggyweb
    Jun 24, 2014 at 10:30
0

Below is my amended code, which addresses my issue. It would appear I wasn't calling the second stored procedure, therefore the DataTable tbl was not being filled with a second dataset.

string name = "";
string password = "";

var stringWriter = new StringWriter();
var writer = new XmlTextWriter(stringWriter) { Formatting = Formatting.Indented };    

var tbl = new DataTable();

string strConn = WebConfigurationManager.ConnectionStrings["TestConnection"].ToString();

var conn = new SqlConnection(strConn);
using (conn)
{
    conn.Open();
    var cmd = new SqlCommand("GetUserLoginID", conn) {CommandType = CommandType.StoredProcedure};
    cmd.Parameters.AddWithValue("@UID", uid);
    cmd.Parameters.AddWithValue("@BID", bId);

    var userRecord = new SqlDataAdapter(cmd);

    userRecord.Fill(tbl);

    if (tbl.Rows.Count > 0)
    {
        var row = tbl.Rows[0];

        name = row["Name"].ToString();
        password = row["Password"].ToString();
        code = row["Code"].ToString();
    }

    tbl.Clear();

    cmd = new SqlCommand("GetUserDetails", conn) { CommandType = CommandType.StoredProcedure };
    cmd.Parameters.AddWithValue("@Code", code);
    cmd.Parameters.AddWithValue("@BID", bId);

    userRecord = new SqlDataAdapter(cmd);

    userRecord.Fill(tbl);

    if (tbl.Rows.Count > 0)
    {
        var row = tbl.Rows[0];

        writer.WriteStartDocument();
        writer.WriteStartElement("Root");
        writer.WriteElementString("BID", row["BID"].ToString());
        writer.WriteElementString("Name", name);

    }
    else
    {
        writer.WriteStartDocument();
        writer.WriteStartElement("Root");
        writer.WriteElementString("BID", "Error");
    }
    writer.WriteEndElement();
    writer.Flush();
    writer.Close();

    conn.Close();
}
tbl.Dispose();

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.