Rohan Rao
Rohan Rao

Reputation: 2603

The DataSet result is always empty in C#

I am retrieving particular column value using DataSet.

This is my code:

public DataSet GetRedirectURL(string emailId)
{
    DataSet ds = new DataSet();
    using (SqlConnection con = new SqlConnection(@"Connection_String_Here"))
    {
        con.Open();
        SqlCommand sqlComm = new SqlCommand("usp_Login", con)
        {
            CommandType = CommandType.StoredProcedure
        };
        sqlComm.Parameters.AddWithValue("@EmailId", emailId);

        SqlDataAdapter da = new SqlDataAdapter
        {
            SelectCommand = sqlComm
        };

        da.Fill(ds);
    }
    return ds;
}

I have also used DataTable instead of it but the same result. I have checked my Stored Procedure and when I pass Parameter it shows Data. So, nothing wrong with the SP. But the Table doesn't show any data and is always empty as shown below:

enter image description here

What am I missing? Any help would be appreciated.

Upvotes: 1

Views: 815

Answers (1)

Matias Bello
Matias Bello

Reputation: 50

I'd suggest you to first evaluate the content of your DataSet. For instance, type in your Immediate Window (or add a quick watch) to check ds.Tables[0].Rows.Count. Basically, to assert your DataSet has been properly filled with the contents fetched from the database, and focus on the data assignment from the DataSet to the grid object you're using to display it.

Also, the .Fill() method has a returning object which is an int representing the amount of rows that have been successfully filled into the target object. For instance:

int result = da.Fill(ds);

Check the value of result after the .Fill() method has been executed.

Finally, I guess you're using a DataGridView object to visualize the results. If so, how are you binding data? Should be something like:

dataGridView1.DataSource = ds.Tables[0];

PS: As I've read in other comments, no, you don't need to execute the .Open() method on the connection. That's not necesarry, it's done implicitely when using the (using SqlConnection conn = SqlConnection..)

Upvotes: 1

Related Questions