Sql server 将存储过程结果传递给textbox

Sql server 将存储过程结果传递给textbox,sql-server,vb.net,Sql Server,Vb.net,我有以下存储过程: ALTER PROCEDURE [dbo].[GetGrossIncome] @Date1 date, @Date2 date, @GrossTotal int OUTPUT AS BEGIN SELECT @GrossTotal = sum(GrandTotal) From ClientInvoicedItems where (InvoicePaidStatus ='FULLY PAID' and (Date >=@date1 and Date <=@Dat

我有以下存储过程:

ALTER PROCEDURE [dbo].[GetGrossIncome]
@Date1 date, @Date2 date, @GrossTotal int OUTPUT
AS
BEGIN

SELECT @GrossTotal =  sum(GrandTotal) From ClientInvoicedItems where (InvoicePaidStatus ='FULLY PAID' and (Date >=@date1 and Date <=@Date2)) group by GrandTotal 

END
当我运行代码时,如果我选择与开始和结束日期相同的日期,我会在文本框中得到一个值,但它总是错误的值。当我选择不同的开始日期和结束日期时,会出现错误:“System.InvalidCastException:”从类型“DBNull”到类型“String”的转换无效。”


欢迎使用指向正确方向的指针,提前感谢。

我不确定当天的值是否不正确,但这就是为什么会出现空错误:

cmd.Parameters.AddWithValue("@date1", TxtDateTo.Text)
cmd.Parameters.AddWithValue("@date2", TxtDateFrom.Text)

(Date >=@date1 and Date <=@Date2))
cmd.Parameters.AddWithValue(“@date1”,TxtDateTo.Text)
cmd.Parameters.AddWithValue(“@date2”,TxtDateFrom.Text)

(Date>=@date1和Date='2021-02-03'以及Date我不确定当天的值是否不正确,但这就是为什么会出现空错误:

cmd.Parameters.AddWithValue("@date1", TxtDateTo.Text)
cmd.Parameters.AddWithValue("@date2", TxtDateFrom.Text)

(Date >=@date1 and Date <=@Date2))
cmd.Parameters.AddWithValue(“@date1”,TxtDateTo.Text)
cmd.Parameters.AddWithValue(“@date2”,TxtDateFrom.Text)

(Date>=@date1和Date='2021-02-03'以及Date,因此我设法使这两组代码都能工作。多亏了成员的建议。存储过程编写错误,下面是正确的。多亏了此链接中的解决方案:


因此,我设法使这两组代码都正常工作。多亏了成员的建议。存储过程编写错误,下面是正确的。多亏了此链接中的解决方案:


某些数据库对象(如连接和命令)需要处理。
Using…End-Using
块即使出现错误也会处理此问题

之前不要打开连接。执行…

不要对Sql Server使用
.AddWithValue
。请参阅 和 还有一个: 这是另一个

我已经将按钮中的用户界面代码和函数中的数据访问代码分开。所有输入和消息框的验证都在按钮代码中

我不确定Sql Server中的参数名称是否区分大小写,但是,仅为了GRIN,我将它们匹配。@date1->@date1等。我认为存储过程中不需要
Group By
短语,因为
Sum()
Select
中的唯一元素

您不需要检查null,因为即使没有符合条件的记录,
Sum()
也将返回0

Private Function GetTotalIncome(startDate As Date, endDate As Date) As Integer
    Dim TotalIncome As Integer
    Dim conn_String = "Data Source=Server-Pc;Initial Catalog=AWInformationSystem;Persist Security Info=True;User ID=sa;Password=password1"
    Using Mycon = New SqlConnection(conn_String),
            cmd As SqlCommand = New SqlCommand("GetGrossIncome", Mycon)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add("@Date1", SqlDbType.Date).Value = startDate
        cmd.Parameters.Add("@Date2", SqlDbType.Date).Value = endDate
        cmd.Parameters.Add("@GrossTotal", SqlDbType.Int)
        cmd.Parameters("@GrossTotal").Direction = ParameterDirection.Output
        Mycon.Open()
        cmd.ExecuteNonQuery()
        TotalIncome = CInt(cmd.Parameters("@GrossTotal").Value)
    End Using
    Return TotalIncome
End Function

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim Date1, Date2 As Date

    If Not Date.TryParse(TxtDateFrom.Text, Date1) Then 'Start Date
        MessageBox.Show("Please enter a valid date in the From box")
        Exit Sub
    End If
    If Not Date.TryParse(TxtDateTo.Text, Date2) Then 'End Date
        MessageBox.Show("Please enter a valid date in the To box.")
        Exit Sub
    End If
    If Date1 > Date2 Then
        MessageBox.Show("The start date cannot be greater than the end date.")
        Exit Sub
    End If
    TxtGrossIncome.Text = GetTotalIncome(Date1, Date2).ToString
End Sub

某些数据库对象(如连接和命令)需要处理。
Using…End-Using
块即使出现错误也会处理此问题

之前不要打开连接。执行…

不要对Sql Server使用
.AddWithValue
。请参阅 和 还有一个: 这是另一个

我已经将按钮中的用户界面代码和函数中的数据访问代码分开。所有输入和消息框的验证都在按钮代码中

我不确定Sql Server中的参数名称是否区分大小写,但是,仅为了GRIN,我将它们匹配。@date1->@date1等。我认为存储过程中不需要
Group By
短语,因为
Sum()
Select
中的唯一元素

您不需要检查null,因为即使没有符合条件的记录,
Sum()
也将返回0

Private Function GetTotalIncome(startDate As Date, endDate As Date) As Integer
    Dim TotalIncome As Integer
    Dim conn_String = "Data Source=Server-Pc;Initial Catalog=AWInformationSystem;Persist Security Info=True;User ID=sa;Password=password1"
    Using Mycon = New SqlConnection(conn_String),
            cmd As SqlCommand = New SqlCommand("GetGrossIncome", Mycon)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add("@Date1", SqlDbType.Date).Value = startDate
        cmd.Parameters.Add("@Date2", SqlDbType.Date).Value = endDate
        cmd.Parameters.Add("@GrossTotal", SqlDbType.Int)
        cmd.Parameters("@GrossTotal").Direction = ParameterDirection.Output
        Mycon.Open()
        cmd.ExecuteNonQuery()
        TotalIncome = CInt(cmd.Parameters("@GrossTotal").Value)
    End Using
    Return TotalIncome
End Function

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim Date1, Date2 As Date

    If Not Date.TryParse(TxtDateFrom.Text, Date1) Then 'Start Date
        MessageBox.Show("Please enter a valid date in the From box")
        Exit Sub
    End If
    If Not Date.TryParse(TxtDateTo.Text, Date2) Then 'End Date
        MessageBox.Show("Please enter a valid date in the To box.")
        Exit Sub
    End If
    If Date1 > Date2 Then
        MessageBox.Show("The start date cannot be greater than the end date.")
        Exit Sub
    End If
    TxtGrossIncome.Text = GetTotalIncome(Date1, Date2).ToString
End Sub


从类型DBNull到类型String的转换无效。
告诉您错误。
DBNull
是从存储过程返回的值,给定这些场景。我会调试,获取
TxtDateTo.Text
TxtDateFrom.Text
的值,如果可以的话,手动运行存储过程。我还会t您的存储过程也是如此,因为这很可能是错误的罪魁祸首。不是VB.net代码。我确实发布了存储过程。DBNull转换错误是因为您注释掉了null检查。如果没有符合条件的行,DBNull将返回。关于错误的结果,可能是因为您传递的是字符串参数而不是日期。以及传递强类型的日期时间参数。
TxtDateTo.Text
建议使用(自由类型)正在使用文本框,而不是日期选择器。这意味着用户可以输入类似
04/02/2020
的值,而您不知道它们是指2020年2月4日还是2020年4月2日。对于日期,您通常最好强制输入格式(这是明确的),或者使用(日期)选择器。
从类型DBNull到类型String的转换无效。
正在告诉您错误。
DBNull
是从存储过程返回的值,给定这些场景。我要调试,获取
TxtDateTo.Text
TxtDateFrom.Text
的值,如果可以,手动运行存储过程。我愿意也要发布存储过程,因为这很可能是错误的罪魁祸首。不是VB.net代码。我发布了存储过程。DBNull转换错误是因为您注释掉了null检查。如果没有符合条件的行,DBNull将返回。关于错误的结果,可能是因为您传递的是字符串参数而不是日期s、 并传递强类型的日期时间参数。
TxtDateTo.Text
建议使用(自由类型)正在使用文本框,而不是日期选择器。这意味着用户可以输入类似
04/02/2020
的值,而您不知道它们是指2020年2月4日还是2020年4月2日。对于日期,您通常最好强制输入格式(这是明确的),或者使用(日期)picker.谢谢,我不知道我为什么错过了它。它解决了部分问题。谢谢,我不知道我为什么错过了它。它解决了部分问题。IsNull业务从何而来。我没有在您提供的链接中看到它。我相信Sum()即使没有符合条件的记录,也将返回0。请查看我的代码,因为您在关闭和处理数据库对象时错过了时机。如果
cmd.ExecuteNonQuery
中有错误,您的连接是如何关闭的,更不用说处理的了。@Mary我运行了您的代码,但每次它都会返回一个值零,无论日期是什么nge
    Dim Mycon = New SqlConnection(conn_String)

    Mycon.Open()
    Dim cmd As SqlCommand = New SqlCommand
    cmd.CommandType = CommandType.StoredProcedure
    cmd.CommandText = "GetGrossIncome"
    cmd.Parameters.AddWithValue("@date1", TxtDateFrom.Text)
    cmd.Parameters.AddWithValue("@date2", TxtDateTo.Text)
    Dim sum_values As SqlParameter = New SqlParameter
    sum_values = cmd.Parameters.Add("@GrossTotal", SqlDbType.Decimal)
    sum_values.Direction = ParameterDirection.Output
    cmd.Connection = Mycon
    cmd.ExecuteNonQuery()
    Mycon.Close()
    TxtGrossIncome.Text = sum_values.Value.ToString()
Private Function GetTotalIncome(startDate As Date, endDate As Date) As Integer
    Dim TotalIncome As Integer
    Dim conn_String = "Data Source=Server-Pc;Initial Catalog=AWInformationSystem;Persist Security Info=True;User ID=sa;Password=password1"
    Using Mycon = New SqlConnection(conn_String),
            cmd As SqlCommand = New SqlCommand("GetGrossIncome", Mycon)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add("@Date1", SqlDbType.Date).Value = startDate
        cmd.Parameters.Add("@Date2", SqlDbType.Date).Value = endDate
        cmd.Parameters.Add("@GrossTotal", SqlDbType.Int)
        cmd.Parameters("@GrossTotal").Direction = ParameterDirection.Output
        Mycon.Open()
        cmd.ExecuteNonQuery()
        TotalIncome = CInt(cmd.Parameters("@GrossTotal").Value)
    End Using
    Return TotalIncome
End Function

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim Date1, Date2 As Date

    If Not Date.TryParse(TxtDateFrom.Text, Date1) Then 'Start Date
        MessageBox.Show("Please enter a valid date in the From box")
        Exit Sub
    End If
    If Not Date.TryParse(TxtDateTo.Text, Date2) Then 'End Date
        MessageBox.Show("Please enter a valid date in the To box.")
        Exit Sub
    End If
    If Date1 > Date2 Then
        MessageBox.Show("The start date cannot be greater than the end date.")
        Exit Sub
    End If
    TxtGrossIncome.Text = GetTotalIncome(Date1, Date2).ToString
End Sub