Hi,
I am working on a project and I am having trouble with this error(see subject line). This happens when I try to select a country from a combo box and what should happen is that when I do this there is a stored procedure that is called in my sql database which reads form a table that has data but then I get this error which suggests otherwise,any ideas?Thnks
Here is where the error occurs:
using (SqlConnection conn = new SqlConnection(ConnString))
{
SqlCommand cmd = new SqlCommand("GetGameForCountry", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Country", match);
//game is a class in my program Game aGame;
try
{
conn.Open();
}
catch
{
}
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow);
reader.Read();
// error here>>> aGame = new Game(reader["ID"].ToString(), reader["Location"].ToString(), reader["DateTime"].ToString());
reader.Close();
}
return aGame;
Loading

Prabhu RajaRank #861 · 2.6K reputationPosted Oct 10, 2011, 2:30 AM EDT
{
}
}
return aGame;
Javeed M ShaikhRank #839 · 2.7K reputationPosted Oct 10, 2011, 2:35 AM EDT
if (Reader.Read())
{
......
}
Please do not forget to mark "Accepted Answer".
Avuya MxoliRank #7,940 · 231 reputationPosted Oct 10, 2011, 2:53 AM EDT
Prabhu RajaRank #861 · 2.6K reputationPosted Oct 10, 2011, 2:58 AM EDT
if( reader.HasRows)
{
{
}
}
Prabhu RajaRank #861 · 2.6K reputationPosted Oct 10, 2011, 3:04 AM EDT
if( reader.HasRows)
Avuya MxoliRank #7,940 · 231 reputationPosted Oct 10, 2011, 3:13 AM EDT
Prabhu RajaRank #861 · 2.6K reputationPosted Oct 10, 2011, 3:25 AM EDT
Pravin MoreRank #1,419 · 1.2K reputationPosted Oct 10, 2011, 5:07 AM EDT
try this code......
String ID="",Location ="",DateTime ="";
while( reader.Read())
{
if(reader.IsDBNull(0))
ID="";
else
ID = reader[0].ToString();
if(reader.IsDBNull(1))
Location ="";
else
Location = reader[1].ToString();
if(reader.IsDBNull(2))
DateTime ="";
else
DateTime = reader[2].ToString();
aGame = new Game(ID, Location, DataTime);
}
hope it will help you.
Thanx,
Pravin.
Avuya MxoliRank #7,940 · 231 reputationPosted Oct 10, 2011, 6:13 AM EDT
Javeed M ShaikhRank #839 · 2.7K reputationPosted Oct 10, 2011, 7:00 AM EDT
SELECT ID,Location,[DateTime] FROM Game WHERE ID LIKE @Country + '%' OR ID LIKE '%' + @Country;
If you are passing a Unicode string i.e. NCHAR,NVARCHAR or NTEXT as compared to CHAR,VARCHAR or TEXT you may see the T-SQL code passes string prefix with 'N' (N actually stands for National language character set)
Please do not forget to mark "Accepted Answer".
Prabhu RajaRank #861 · 2.6K reputationPosted Oct 10, 2011, 9:22 AM EDT
I have created a Table :
Table Name : Game
Columns :
ID nvarchar(50)
Location nvarchar(50)
DateTime SmallDateTime.
After that i have Inserted Values to Table. Currently table contains
ID Location DateTime
Ind India 1/1/2011
11Ind Cape Town 1/1/2011
And My procedure is,
And Executed As,
The Output i got,
ID Location DateTime
Ind India 1/1/2011
11Ind Cape Town 1/1/2011
Everything is correct. I think in your Table, ID Column may not contain France String. Try to Pass Only N'rance'.
Suthish NairRank #92 · 22.8K reputationPosted Oct 11, 2011, 10:29 AM EDT
Avuya MxoliRank #7,940 · 231 reputationPosted Oct 11, 2011, 2:00 PM EDT
Prabhu RajaRank #861 · 2.6K reputationPosted Oct 11, 2011, 9:30 PM EDT
But i suggest you to try like
// Consider this, we have initialized Game Class Object as
// aGame = new Game(reader["ID"].ToString(), reader["Location"].ToString(), reader["DateTime"].ToString());
// Here we passing ID, Location and DateTime to Game Class Constructor, And Constructor will initialize Local Variable with these Values. So, you can //use those variables like this.
Avuya MxoliRank #7,940 · 231 reputationPosted Oct 11, 2011, 9:40 PM EDT
Prabhu RajaRank #861 · 2.6K reputationPosted Oct 11, 2011, 9:43 PM EDT
Avuya MxoliRank #7,940 · 231 reputationPosted Oct 11, 2011, 9:48 PM EDT
Prabhu RajaRank #861 · 2.6K reputationPosted Oct 11, 2011, 9:55 PM EDT
http://msdn.microsoft.com/en-us/library/fkx0cy6d.aspx
Avuya MxoliRank #7,940 · 231 reputationPosted Oct 11, 2011, 9:58 PM EDT
Prabhu RajaRank #861 · 2.6K reputationPosted Oct 11, 2011, 10:40 PM EDT
// Please check sGame is null or not, before used
if ( aGame== null)
dgvGame.DataSource = aGame;
else
MessageBox.Show("Object is null");
Avuya MxoliRank #7,940 · 231 reputationPosted Oct 12, 2011, 2:52 AM EDT
Prabhu RajaRank #861 · 2.6K reputationPosted Oct 12, 2011, 4:15 AM EDT
execute stored procedure in Sql Management Studio for some parameter value to check, if it returns values or not, then use the same value in .net code as Parameter to that stored procedure.
Avuya MxoliRank #7,940 · 231 reputationPosted Oct 12, 2011, 12:15 PM EDT
Prabhu RajaRank #861 · 2.6K reputationPosted Oct 13, 2011, 1:06 AM EDT
Please Try this.
try
{
SqlConnection conn = new SqlConnection(ConnString))
SqlCommand cmd = new SqlCommand("GetGameForCountry");
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
cmd.Connection = con;
cmd.Parameters["@Country"].value = match.Trim();
SqlDataReader reader = cmd.ExecuteReader();
{
}
catch
{
}
return aGame;
Avuya MxoliRank #7,940 · 231 reputationPosted Oct 13, 2011, 4:17 AM EDT
An SqlParameter with ParameterName '@Country' is not contained by this SqlParameterCollection.
Prabhu RajaRank #861 · 2.6K reputationPosted Oct 15, 2011, 10:16 PM EDT
Suthish NairRank #92 · 22.8K reputationPosted Oct 15, 2011, 11:54 PM EDT