将记录检索到vb.net中的组合框

时间:2016-01-03 14:54:56

标签: sql-server vb.net sql-server-2008 combobox

我有一个存储过程,我想将记录显示到combobox。但它说的是

  

此行的0位没有行。

cboSchoolYear.Text = (dt.Rows(0)("Schoolyear")) 

存储过程代码:

ALTER PROCEDURE [dbo].[uspLatestDateEnrolled]
    -- Add the parameters for the stored procedure here

@studID INT


AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT TOP 1 DateEnrolled as LatestDate,
    SchoolYear,Levels,Section,StudentID
    FROM StudentHistory
    WHERE studentID = @studID
    ORDER BY DateEnrolled DESC
END

Vb.net代码

   cn.Open()
        Using cmd As New SqlClient.SqlCommand("uspLatestDateEnrolled", cn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.Add(New SqlParameter("@studID", frmView.dgv1.SelectedCells(0).Value))
            cboSchoolYear.Text = (dt.Rows(0)("Schoolyear"))
            cboGradeLevel.Text = (dt.Rows(1)("levels"))
            cboSection.Text = (dt.Rows(2)("Section"))
            dtpEnrollment.Text = (dt.Rows(3)("dateEnrolled"))
        End Using
        cn.Close()

2 个答案:

答案 0 :(得分:0)

在做一些研究时,这段代码解决了我的问题。感谢Codexer,给我一些提示。

  Try
        cn.Open()
        cmd = New SqlCommand("uspLatestDateEnrolled", cn)
        cmd.Parameters.AddWithValue("@studID", frmView.dgv1.SelectedCells(0).Value)
        cmd.CommandType = CommandType.StoredProcedure
        da.SelectCommand = cmd
        da.Fill(dt)
        cboSchoolYear.Text = dt.Rows(0).Item("SchoolYear")
        cboGradeLevel.Text = dt.Rows(0).Item("levels")
        cboSection.Text = dt.Rows(0).Item("Section")
        dtpEnrollment.Text = dt.Rows(0).Item("DateEnrolled")

    Catch x As Exception
        MessageBox.Show(x.GetBaseException().ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
    Finally
        cmd.Dispose()
        cn.Close()
    End Try

答案 1 :(得分:-1)

你在这里遇到了一些错误的事情(或者有些事情是错误的,并且帖子中缺少一些东西)

  1. 你永远不会宣布DT
  2. 你正在跳行,如果你返回了多于一个结果(你不会因为你的sql中的前1名你将获得不同记录中的信息)
  3. 您不会检查是否从数据库中返回任何内容
  4. 你的vb.net代码看起来应该是这样的(这段代码并不完美,但它应该让你朝着正确的方向前进)

        cn.Open()
        Using cmd As New SqlClient.SqlCommand("uspLatestDateEnrolled", cn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.Add(New SqlParameter("@studID", frmView.dgv1.SelectedCells(0).Value))
    
        Dim SQLDataReader as SQLDataReader = cmd.executeReader()
    
        if sqldatareader.hasrows = true then 
        while sqldatareader.read
    
            if sqldatareader("Schoolyear") isnot dbnull.value then 
                    cboSchoolYear.Text = sqldatareader("Schoolyear")
            else
                    cboSchoolYear.Text = "Null"
            end if
            if sqldatareader("levels") isnot dbnull.value then 
                    cboGradeLevel.Text = sqldatareader("levels")
            else
                    cboGradeLevel.Text = "Null"
            end if
            if sqldatareader("Section") isnot dbnull.value then 
                    cboSection.Text = sqldatareader("Section")
            else
                    cboSection.Text = "Null"
            end if
            if sqldatareader("dateEnrolled") isnot dbnull.value then 
                    cboSection.Text = sqldatareader("dateEnrolled")
            else
                    cboSection.Text = "Null"
            end if
    
        loop
        else
           'No Results
        end if 
    
        End Using
        cn.Close()