mercredi 1 avril 2015

Why Isn't my datagridview working?

I am trying to make an application that checks call info from a SQL database. However I am having some issues getting the SQL query results into a display. I am currently trying to use a DataGridView to push the results to but I keep getting an object reference error in my code and while I know I can't call a form element from outside it's class I can't figure out how to get it to work either. What I need to do is click the submit button, do some logic (which I haven't added yet as I am testing query results display first), then display the results to a view. My connection works and I have been able to display info using reader to lines in a console application but can't get it to work here.



private void Submit_Click(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection("Server=...; Database=...; Integrated Security=True;");

try
{
myConnection.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

try
{
string select = "SELECT Top 10 * FROM myTable";
SqlDataAdapter dataAdapter = new SqlDataAdapter(select, myConnection);

SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
dataDisplay.ReadOnly = true;
DataGridView.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}


Edit for info The line DataGridView.DataSource = ds.Tables[0]; is throwing the error. As for naming I will adjust names later, I am fairly new to C# and trying to get this to work before I worry about things like that.


Aucun commentaire:

Enregistrer un commentaire