Reputation: 49
I am trying to pass a single input parameter to a stored procedure that, I thought, had OUTPUT parameters declared, but seems to be input/output variables, thus giving me an error message that states that one of the parameters was not provided a value.
The C# calling code is set up as follows:
protected void CheckBoxClassRegion_btnSubmit(object sender, EventArgs e)
{
date @date; /* Variable for the date of the game */
varchar @HomeTeam; /* The name of the high school team */
varchar @AwayTeam; /* The name of the other H.s. team */
int @TeamID; /* The ID number of the high school team */
AddressText.Text = "";
/**********************************************************************/
/* The code below will initialize the connection to the database. */
/* As the connection string to the SQL database is defined as conn, */
/* the open method from conn will connect to the database, and the */
/* cmd variable will call on the stored procedure GetSchedule. */
/**********************************************************************/
string strcon = WebConfigurationManager.ConnectionStrings["FollowingHSFootballConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(strcon);
conn.Open();
SqlCommand cmd = new SqlCommand("GetSchedule", conn);
cmd.CommandType = CommandType.StoredProcedure;
/**********************************************************************/
/* The for loop below will determine which items from the checkbox */
/* were selected from the input and use the High School team name to */
/* pass to the stored procedure 'GetSchedule' to return the dates, */
/* home team and away team each game. */
/**********************************************************************/
/**********************************************************************/
foreach (ListItem item in CheckBoxClassRegion.Items) /* This loop will go through all of the checkboxed items */
{ /**********************************************************************/
if (item.Selected == true) /* If this team has been selected */
{ /**********************************************************************/
cmd.Parameters.AddWithValue("@TeamName", item.Text); /* Pass input parameter "Team Name" */
SqlDataReader reader = cmd.ExecuteReader(); /* Utilize the reader function to ensure all games are included */
while (reader.Read()) /* While there are still items to be read */
{ /**********************************************************************/
cmd.Parameters.Add("@date", SqlDbType.Date);
cmd.Parameters["@date"].Direction = ParameterDirection.Output;
cmd.Parameters.Add("@HomeTeam", SqlDbType.VarChar, 25);
cmd.Parameters["@HomeTeam"].Direction = ParameterDirection.Output;
cmd.Parameters.Add("@AwayTeam", SqlDbType.VarChar, 25);
cmd.Parameters["@AwayTeam"].Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery(); /* Execute the stored procedure */
Console.WriteLine(cmd.Parameters["@GameDate"].Value);
Console.WriteLine(cmd.Parameters["@HomeTeam"].Value);
Console.WriteLine(cmd.Parameters["@AwayTeam"].Value);
Console.ReadLine();
}
Stored Procedure:
ALTER PROCEDURE [dbo].[GetSchedule]
@teamname varchar(25),
@date Date OUTPUT,
@HomeTeam varchar(25) OUTPUT,
@AwayTeam varchar(25) OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SELECT HomeSchedule.Date, HomeTeam.HighSchoolName, AwayTeam.HighSchoolName
from (
(Schedule$ as HomeSchedule inner join HighSchoolFootballTeam$ as HomeTeam
on HomeSchedule.HomeTeamID = HomeTeam.HighSchoolTeamID)
inner join
(Schedule$ as AwaySchedule inner join HighSchoolFootballTeam$ as AwayTeam
on AwaySchedule.AwayTeamID = AwayTeam.HighSchoolTeamID)
on HomeSchedule.GameID = AwaySchedule.GameID)
where HomeTeam.HighSchoolName = @teamname or AwayTeam.HighSchoolName = @teamname
Order by HomeSchedule.Date
END
How do I get the output variables of @Date
, @HomeTeam
and @AwayTeam
to act as just output and not input/output variables so that the stored procedure does not expect an input value from them?
Upvotes: 1
Views: 2707
Reputation: 14624
Your stored procedure has @teamname
parameter that's set as an input parameter, but you're not setting any value to @teamname
parameter in your C# code, so that's why you got the error. You need to set a value to @teamname
parameter as below.
while (reader.Read()) /* While there are still items to be read */
{ /**********************************************************************/
cmd.Parameters.Add("@teamname", SqlDbType.VarChar, 25);
cmd.Parameters["@teamname"].Value = ...; // set the value here
cmd.Parameters.Add("@date", SqlDbType.Date);
cmd.Parameters["@date"].Direction = ParameterDirection.Output;
cmd.Parameters.Add("@HomeTeam", SqlDbType.VarChar, 25);
cmd.Parameters["@HomeTeam"].Direction = ParameterDirection.Output;
cmd.Parameters.Add("@AwayTeam", SqlDbType.VarChar, 25);
cmd.Parameters["@AwayTeam"].Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery(); /* Execute the stored procedure */
Console.WriteLine(cmd.Parameters["@GameDate"].Value);
Console.WriteLine(cmd.Parameters["@HomeTeam"].Value);
Console.WriteLine(cmd.Parameters["@AwayTeam"].Value);
Console.ReadLine();
}
EDIT
After looking at the more complete code, here's your mistake
cmd.Parameters.AddWithValue("@TeamName", item.Text);
SqlDataReader reader = cmd.ExecuteReader();
You haven't added @date
, @HomeTeam
, and @AwayTeam
parameters when you do SqlDataReader reader = cmd.ExecuteReader();
, that's why you got the error. You should add those three parameters before executing cmd.ExecuteReader()
. You also need to clear the parameters of cmd
using command.Parameters.Clear();
since you're using cmd
inside a loop and remove cmd.ExecuteNonQuery()
since you're already executing the stored procedure when calling cmd.ExecuteReader()
. Change your code as below
if (item.Selected == true)
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@TeamName", item.Text);
cmd.Parameters.Add("@date", SqlDbType.Date);
cmd.Parameters["@date"].Direction = ParameterDirection.Output;
cmd.Parameters.Add("@HomeTeam", SqlDbType.VarChar, 25);
cmd.Parameters["@HomeTeam"].Direction = ParameterDirection.Output;
cmd.Parameters.Add("@AwayTeam", SqlDbType.VarChar, 25);
cmd.Parameters["@AwayTeam"].Direction = ParameterDirection.Output;
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(cmd.Parameters["@GameDate"].Value);
Console.WriteLine(cmd.Parameters["@HomeTeam"].Value);
Console.WriteLine(cmd.Parameters["@AwayTeam"].Value);
Console.ReadLine();
}
Upvotes: 4