尝试填充List <>时如何解决“必须声明标量变量”错误

时间:2019-04-15 01:18:52

标签: c# sql asp.net

我正在尝试使用存储过程填充TextBox。我不断收到错误

  

必须声明标量变量“ @programId”

对于为什么发生这种情况的任何帮助,将不胜感激。

这就是我所拥有的:

CREATE PROCEDURE [dbo].[ProgramCourses_FindByProgramAndCourse]
    @programid INT,
    @courseid NVARCHAR(7)
AS
BEGIN
    SET NOCOUNT ON;      

    SELECT 
        ProgramCourseID, 
        ProgramID, 
        CourseID, 
        Required, 
        Comments,
        SectionLimit,
        Active
    FROM
        ProgramCourses
    WHERE 
        ProgramID = @programid
        AND CourseID = @courseid
END
RETURN 

这是我的C#:

public List<ProgramCourse> GetProgamCourseIdBySP(int programId, string courseId)
{
        using (var context = new StarTEDContext())
        {
            string storedProcSql = "EXEC ProgramCourses_FindByProgramAndCourse @programId, @courseId";

            var results = context.Database.SqlQuery<ProgramCourse>(storedProcSql, programId, courseId);

            //var results = context.Database.SqlQuery<ProgramCourse>("EXEC ProgramCourses_FindByProgramAndCourse @programId, @courseId", programId, courseId);
            return results.ToList();
        }
}

public ProgramCourse ProgramCourse_Find(List<ProgramCourse> programCourseId)
{
        using (var context = new StarTEDContext())
        {
            return context.ProgramCourses.Find(programCourseId);
        }
}

int programId和字符串courseId来自此代码块

var courseId = CourseLookupDropDown.SelectedValue;
var programId = ProgramDropDown.SelectedIndex;

var controllerPC = new ProgramCourseController();

var programCourseIdSP = controllerPC.GetProgamCourseIdBySP(programId, courseId);

var PCID = controllerPC.ProgramCourse_Find(programCourseIdSP);

ProgramCourseID.Text = PCID.ProgramCourseID.ToString();
PCProgramID.Text = PCID.ProgramID.ToString();
PCCourseID.Text = PCID.CourseID.ToString();
Required.Text = PCID.Required.ToString();
Comments.Text = PCID.Comments;
SelectionLimit.Text = PCID.SectionLimit.ToString();
Active.Text = PCID.Active.ToString();

使用此解决。...

public ProgramCourse GetProgamCourseIdBySP(int programId, string courseId)
{
    using (var context = new StarTEDContext())
    {
        ProgramCourse programCourse = new ProgramCourse();
        programCourse = context.ProgramCourses.Where(x => x.ProgramID == programId && x.CourseID == courseId).Single();

        return programCourse;
    }
}

0 个答案:

没有答案