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?