简体   繁体   中英

DataTable Always Returns 0

My query returns results, but for some reason my DataTable always shows 0. The only thing I altered was the fact that I added parameters to the C# syntax (altho if I manually run the stored procedure it returns results). This is my syntax, does anyone see something that is incorrect syntactically in it?

protected void btnPress1464()
{
    RunSQLStoredProc();
    DataTable tableA = ebdb.Tables[0];
    if (this.dtgAttendanceTracker.Items.Count == 0)
    {
        this.gvwTest.DataSource = tableA
        this.gvwTest.DataBind();
    }
}

public DataSet RunSQLStoredProc()
{
    ebdb = new DataSet(); 
    SqlQueryBuilder = new StringBuilder();
    SqlQueryBuilder.Append("exec alphadawg ");
    ebdb = DoThis(SqlQueryBuilder.ToString());
    return ebdb;
}

public DataSet DoThis(string sqlQuery, int employeeid, DateTime hiredate, DateTime terminationdate)
{
    try
    {
        System.Configuration.ConnectionStringSettings connstring = System.Configuration.ConfigurationManager.ConnectionStrings["SQLServer1"];

        using (SqlConnection conn = new SqlConnection(connstring.ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = sqlQuery;
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@employeeid", employeeid.ToString());
                cmd.Parameters.AddWithValue("@hiredate", hiredate.ToShortDateString());
                cmd.Parameters.AddWithValue("@terminationdate", terminationdate.ToShortDateString());
                conn.Open();
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);                    
                adapter.Fill(ebdb);
                conn.Close();
            }                
        }
        return ebdb;
    }
    catch (Exception exception) { throw exception; }
}

The CommandText should only contain the stored-procedure name and not also exec if the command's CommandType is StoredProcedure . The StringBuilder is also redundant.

I also think that the way how you use AddWithValue with the wrong types could cause this issue(look at the last paragraph of my answer):

So not

SqlQueryBuilder = new StringBuilder();
SqlQueryBuilder.Append("exec alphadawg ");
ebdb = DoThis(SqlQueryBuilder.ToString());

but

ebdb = DoThis("alphadawg", otherParamaters...);

It's also bad practice to pass a sql-string to a method that executes it, that often introduces sql injection issues. You should not have a method DoThis but GetAlphaDawg which encapsulates the sql-query and only pass the parameter-values.

Apart from that, why do you return the DataSet from a method if it's actually a field in your class that you return? Instead initialize and fill it in the method, that's much clearer and also prevents issues when you load an already filled dataset(data will be appended by default).

This would be a possible implementation. Note that you shouldn't use AddWithValue and don't use String for DateTime but always use the correct type , all the more if you use AddWithValue which needs to infer the type from the value:

public DataSet GetAlphaDawg(int employeeid, DateTime hiredate, DateTime terminationdate)
{
    DataSet dsAlpha = new DataSet();
    try
    {
        System.Configuration.ConnectionStringSettings connstring = System.Configuration.ConfigurationManager.ConnectionStrings["SQLServer1"];

        using (var conn = new SqlConnection(connstring.ConnectionString))
        {
            using (var da = new SqlDataAdapter("alphadawg", conn))
            {
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                var parameter = da.SelectCommand.Parameters;
                parameter.Add("@employeeid", SqlDbType.Int).Value = employeeid;
                parameter.Add("@hiredate", SqlDbType.Date).Value = hiredate;
                parameter.Add("@terminationdate", SqlDbType.Date).Value = terminationdate;
                da.Fill(dsAlpha); // Open/Close not needed with Fill
                return dsAlpha;
            }
        }
    } catch (Exception) { throw; }
}

Since you use ToShortDateString , if you actually want to remove the time portion of your DateTime use DateTime.Date , for example:

parameter.Add("@hiredate", SqlDbType.Date).Value = hiredate.Date;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM