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
Suthish NairPosted Oct 16, 2011, 3:54 AM
Prabhu RajaPosted Oct 16, 2011, 2:16 AM
Avuya MxoliPosted Oct 13, 2011, 8:17 AM
An SqlParameter with ParameterName '@Country' is not contained by this SqlParameterCollection.
Prabhu RajaPosted Oct 13, 2011, 5:06 AM
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 MxoliPosted Oct 12, 2011, 4:15 PM
Prabhu RajaPosted Oct 12, 2011, 8:15 AM
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 MxoliPosted Oct 12, 2011, 6:52 AM
Prabhu RajaPosted Oct 12, 2011, 2:40 AM
// Please check sGame is null or not, before used
if ( aGame== null)
dgvGame.DataSource = aGame;
else
MessageBox.Show("Object is null");
Avuya MxoliPosted Oct 12, 2011, 1:58 AM
Prabhu RajaPosted Oct 12, 2011, 1:55 AM
http://msdn.microsoft.com/en-us/library/fkx0cy6d.aspx
Avuya MxoliPosted Oct 12, 2011, 1:48 AM
Prabhu RajaPosted Oct 12, 2011, 1:43 AM
Avuya MxoliPosted Oct 12, 2011, 1:40 AM
Prabhu RajaPosted Oct 12, 2011, 1:30 AM
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 MxoliPosted Oct 11, 2011, 6:00 PM
Suthish NairPosted Oct 11, 2011, 2:29 PM
Prabhu RajaPosted Oct 10, 2011, 1:22 PM
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'.
Javeed M ShaikhPosted Oct 10, 2011, 11:00 AM
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".
Avuya MxoliPosted Oct 10, 2011, 10:13 AM
Pravin MorePosted Oct 10, 2011, 9:07 AM
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.
Prabhu RajaPosted Oct 10, 2011, 7:25 AM
Avuya MxoliPosted Oct 10, 2011, 7:13 AM
Prabhu RajaPosted Oct 10, 2011, 7:04 AM
if( reader.HasRows)
Prabhu RajaPosted Oct 10, 2011, 6:58 AM
if( reader.HasRows)
{
{
}
}
Avuya MxoliPosted Oct 10, 2011, 6:53 AM
Javeed M ShaikhPosted Oct 10, 2011, 6:35 AM
if (Reader.Read())
{
......
}
Please do not forget to mark "Accepted Answer".
Prabhu RajaPosted Oct 10, 2011, 6:30 AM
{
}
}
return aGame;