'Insert data into table using stored procedure

protected void ButtonNota_Click(object sender, EventArgs e)
{
    string nota = TextBoxNota.Text;
    string materieID = ddl.SelectedValue.ToString();
    string userID = Session["userID"].ToString();
    SqlConnection conn = new SqlConnection("Data Source=localhost\\sqlexpress;Initial Catalog=UsersDB;Integrated Security=SSPI");
    try
    {

        DataTable dt = new DataTable();

        conn.Open();
        using (SqlCommand command = new SqlCommand("insertIntoNote", conn))
        {

            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add("@notaMaterie",System.Data.SqlDbType.Int);
            command.Parameters["@notaMaterie"].Value =Convert.ToInt32(TextBoxNota.Text);

            command.Parameters.Add("@idMaterie", System.Data.SqlDbType.Int);
            command.Parameters["@idMaterie"].Value = Convert.ToInt32(materieID);

            command.Parameters.Add("@userID", System.Data.SqlDbType.Int);
            command.Parameters["@userID"].Value = Convert.ToInt32(userID);
            command.ExecuteNonQuery();

        }
        conn.Close();

    }
    catch (Exception ex)
    {
        // Handle the error
    }
}

}

stored procedure code:

CREATE PROCEDURE [dbo].[instertIntoNote]
    @notaMaterie int,
    @idMaterie int,
    @userID int
AS

BEGIN

INSERT into Note (UserID,MaterieID,Nota) values (@userID,@idMaterie,@notaMaterie)

END

WHY DOESN T IT WORK?

/////

LE: so i managed to insert into database the grades i wanted to but now i have a different problem. the userID and grade iserted is correct but the subjectID doesn t chage (i have math, sports, english so let s say 1, 2, 3... no matter what subject i select from the dropdown the sublectID remains 1... like for math)



Solution 1:[1]

alter procedure insertdata 
as begin
declare @id int,
@name varchar(100);

set @id = (select supplier_id from orders where supplier_id=10004);

set @name =( select firstname from tbl_Students where Studentid=4);

insert into suppliers(supplier_id,supplier_name) values (@id,@name)

end

exec insertdata  -- Use for Execution

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paul Roub