SmallFries BigGuys SmallFries BigGuys - 22 days ago 6
C# Question

Error Trying To Fill Data Table

This is my syntax, but I get an error of


The SelectCommand property has not been initialized before calling 'Fill'.


what do I need to do in order to be able to fill the data table?

using (SqlConnection conn = new SqlConnection("Server=Test;Database=Test;Integrated Security=SSPI;"))
{
SqlCommand command = new SqlCommand();
command.CommandText = "SELECT * FROM [dbo].[selfservice] WHERE saleID = @userid;";
command.Parameters.Add("@userid", SqlDbType.VarChar);
command.Parameters["@userid"].Value = row.Field<string>("saleID");
command.Connection = conn;
using (SqlDataAdapter dataadapter1 = new SqlDataAdapter()
{
dataadapter1.Fill(dtData);
}
}

Answer

Notice that you're not using the command object. You need to add the select command to your adapter:

using (SqlDataAdapter dataadapter1 = new SqlDataAdapter()
{
    dataadapter1.SelectCommand = command
    dataadapter1.Fill(dtData);
}
Comments