Hi,
I am still struggling with the problem I posted earlier this week about filling my gridview. The same procedure works fine when I execute it in sql but in c# it doesnt fill the grid. I get ths exception error when I do this:
DataTable dtGames = new DataTable();
try
{
SqlCommand cmd = new SqlCommand("GetGameForCountry '" + game + "'", conn);
dbAdapter = new SqlDataAdapter(dbCommand);
dbAdapter.Fill(dtGames);
}
catch(SqlException sqlex)
{
MessageBox.Show("SQL exception: " + sqlex.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return dtGames;
I then call this in the combobox text changed event like this:
try
{
dgvGame.DataSource = bl.getGameForCountry(cboCountry.SelectedValue.ToString());
}
catch (SqlException sqlex)
{
MessageBox.Show("SQL exception: " + sqlex.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Any ideas why I am getting this error?Thanks
I am still struggling with the problem I posted earlier this week about filling my gridview. The same procedure works fine when I execute it in sql but in c# it doesnt fill the grid. I get ths exception error when I do this:
DataTable dtGames = new DataTable();
try
{
SqlCommand cmd = new SqlCommand("GetGameForCountry '" + game + "'", conn);
dbAdapter = new SqlDataAdapter(dbCommand);
dbAdapter.Fill(dtGames);
}
catch(SqlException sqlex)
{
MessageBox.Show("SQL exception: " + sqlex.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return dtGames;
I then call this in the combobox text changed event like this:
try
{
dgvGame.DataSource = bl.getGameForCountry(cboCountry.SelectedValue.ToString());
}
catch (SqlException sqlex)
{
MessageBox.Show("SQL exception: " + sqlex.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Any ideas why I am getting this error?Thanks
Avuya MxoliPosted Oct 14, 2011, 10:40 AM
Hi all, my problem has been resolved. As i was filling the combobox I was making use of the .selected value which was actually the value member of the text box. I though I was using the display member all along so to change to value member I changed to use the .text instead. Thanks for all your input oh and Vulpes I changed my name, when I registered I thought they wanted my first name as it is in my ID so I was playing around with my profile yesterday and noticed that I can actually change my name to the name I'm known by
VulpesPosted Oct 13, 2011, 6:18 PM
Sam HobbsPosted Oct 13, 2011, 5:47 PM
Sorry about my comment about GetGameForCountry; I guess you should ignore it.
Avuya MxoliPosted Oct 13, 2011, 5:14 PM
VulpesPosted Oct 13, 2011, 3:05 PM
Avuya MxoliPosted Oct 13, 2011, 2:57 PM
Sam HobbsPosted Oct 13, 2011, 2:44 PM
Also note that you don't show what GetGameForCountry is. If you need help with it, or use of it, then you should show what it is.
Also, it it were me, I would use a TableAdpater or Entity Framework and then you don't have to waste time with details. It is good to know about the details, but there is so much to learn that it is usually better to not dwell on details that you don't have to do. Try my Database Table Update in a DataGridView without Writing Code; it shows use of a TableAdpater. The Entity Framework is newer technology and there are also articles about it.
Avuya MxoliPosted Oct 13, 2011, 1:20 PM
VulpesPosted Oct 13, 2011, 1:05 PM
Avuya MxoliPosted Oct 13, 2011, 12:36 PM
Suthish NairPosted Oct 13, 2011, 12:19 PM
Avuya MxoliPosted Oct 13, 2011, 11:55 AM
VulpesPosted Oct 13, 2011, 11:41 AM
Suthish NairPosted Oct 13, 2011, 10:26 AM
Avuya MxoliPosted Oct 13, 2011, 9:41 AM
VulpesPosted Oct 13, 2011, 9:38 AM
When you said in your opening post that it doesn't fill the grid, did you in fact get one row of data because that's what I'd expect when you call SqlDataAdapter.Fill() with a DataTable argument?
Here's an extract from the MSDN documentation:
"The overload of Fill that takes DataTable as a parameter only obtains the first result. Use an overload of Fill that takes DataSet as a parameter to obtain multiple results."
Avuya MxoliPosted Oct 13, 2011, 9:17 AM
Avuya MxoliPosted Oct 13, 2011, 9:13 AM
VulpesPosted Oct 13, 2011, 5:20 AM
Suthish NairPosted Oct 13, 2011, 5:01 AM
Always use Parameterized Queries..
SqlConnection conn = new SqlConnection(connection string);
SqlDataAdapter dbAdapter = new SqlDataAdapter();
dbAdapter.SelectCommand = new SqlCommand(GetGameForCountry '" + game + "'", conn);
dbAdapter.Fill(dataset);
Pravin MorePosted Oct 13, 2011, 4:52 AM
can you paste exact error message and complite create sproc script here?
Thanx,
Pravin.