sql-server - 在 Visual Studio 2013 中,我只是想使用 VB 将数据传递给 sql 表?

标签 sql-server vb.net visual-studio-2013

我正在设计一个应用程序,这是我的强项,但现在必须编写后端代码。我正在将 Visual Studio 2013 与 DevExpress 和 SQL Server 2014 一起使用。这可能是一个简单的问题,但我一直在努力寻找直接的答案。我有一本 asp.net 的书,但我仍然找不到答案。我想我已经将我的整个解决方案与一个连接字符串连接起来,因为我可以将表和存储过程填充到我的数据类中。我只是想使用带有 onclick 事件的按钮将任何用户输入添加到 SQL Server 上的表中。当然,这不会那么困难,但请记住我是一个新手,所以我们将不胜感激任何帮助。如果您有任何问题需要回答以提供帮助,请告诉我。我正在用 VB 脚本编写,但正在努力让它工作。任何意见,将不胜感激。提前致谢!

     Protected Sub btnAddNewSource_Click(sender As System.Object, e As EventArgs) Handles btnAddNewSource.Click


    'ErrDetails.Text = ""
    'ErrDetails.Visible = False
    'FocusSet = False
    'errCount = 0
    'ErrDetails.Text = ""

    'If txtSourceFunding.Text = "" Then
    '    ErrDetails.Text = ErrDetails.Text + "Did you enter the funding source?" + vbNewLine
    '    ErrDetails.Visible = True
    '    txtSourceFunding.Focus()
    '    FocusSet = True
    '    errCount = errCount + 1
    'End If

    'If txtContributionFunding.Text = "" Then
    '    ErrDetails.Text = ErrDetails.Text + "Did you enter the contribution?" + vbNewLine
    '    ErrDetails.Visible = True
    '    txtContributionFunding.Focus()
    '    FocusSet = True
    '    errCount = errCount + 1
    'End If

    'If cmbStatus.Value = -1 Then
    '    ErrDetails.Text = ErrDetails.Text + "Did you inform us of the status?" + vbNewLine
    '    ErrDetails.Visible = True
    '    cmbStatus.Focus()
    '    FocusSet = True
    '    errCount = errCount + 1
    'End If

    'If FocusSet = True Then
    '    ErrDetails.ForeColor = Drawing.Color.Red
    '    ErrDetails.Height = 20 * errCount
    '    ErrDetails.Visible = True
    '    Return
    'End If

    'Dim btnSource = (From o In dc1.Update_GrantApplicationCycleFunding Where o.GrantApplicationID = Session("CurrentProjectID").ToString).FirstOrDefault
    'If Not IsNothing(btnSource) Then
    '    btnSource.GrantApplicationID = Session("CurrentProjectID")
    '    btnSource.GrantApplicationCycleFundingSource = txtSourceFunding.ToString
    '    btnSource.GrantApplicationCycleFundingContribution = txtContributionFunding.ToString
    '    btnSource.GrantApplicationCycleFundingStatusID = cmbStatus.ToString
    '    btnSource.GrantApplicationCycleFundingNotes = memFundingNotes.ToString

    'Else

    '    Dim NewGrantApplicationCycleFundings As New GrantApplicationCycleFunding
    '    With NewGrantApplicationCycleFundings
    '        .GrantApplicationID = Session("CurrentProjectID")
    '        .GrantApplicationCycleFundingSource = txtSourceFunding.ToString
    '        .GrantApplicationCycleFundingContribution = txtContributionFunding.ToString
    '        .GrantApplicationCycleFundingStatusID = cmbStatus.ToString
    '        .GrantApplicationCycleFundingNotes = memFundingNotes.Text
    '    End With
    '    dc1.GrantApplicationCycleFundings.InsertOnSubmit(NewGrantApplicationCycleFundings)
    '    dc1.SubmitChanges()
    '        End If

    'dc1.SubmitChanges()

最佳答案

嘿,我现在已经设法让按钮将数据添加到数据网格。我错过的最重要的事情是代码底部的字段值声明,当然还有数据绑定(bind)!永远不要忘记正确的 DATABIND。如果您遇到类似问题,可以在自己的代码中改编和使用它!如果您需要帮助或解释,而我可以帮助您,那么我会的。只需添加评论。谢谢

    ** ErrDetails.Text = ""
    ErrDetails.Visible = False
    FocusSet = False
    errCount = 0
    ErrDetails.Text = ""

    If spnTotalEstimatedCost.Value <= 0 Then
        ErrDetails.Text = ErrDetails.Text + "Please enter the cost." + vbNewLine
        ErrDetails.Visible = True
        If FocusSet = False Then
            spnTotalEstimatedCost.Focus()
            FocusSet = True
        End If
        errCount = errCount + 1
    End If

    If txtSourceFunding.Text = "" Then
        ErrDetails.Text = ErrDetails.Text + "Please........?" + vbNewLine
        ErrDetails.Visible = True
        txtSourceFunding.Focus()
        FocusSet = True
        errCount = errCount + 1
    End If

    If spnContributionFunding.Value = 0 Then
        ErrDetails.Text = ErrDetails.Text + "Please......" + vbNewLine
        ErrDetails.Visible = True
        spnContributionFunding.Focus()
        FocusSet = True
        errCount = errCount + 1
    End If

    If FocusSet = True Then
        ErrDetails.ForeColor = Drawing.Color.Red
        ErrDetails.Height = 20 * errCount
        ErrDetails.Visible = True
        Return
    End If

    Dim NewGrantApplicationMatchFundings As New GrantApplicationMatchFunding
    With NewGrantApplicationMatchFundings
        .GrantApplicationID = Session("CurrentProjectID")
        .GrantApplicationMatchFundingName = txtSourceFunding.Text
        .GrantApplicationMatchFundingContribution = spnContributionFunding.Value
        .GrantApplicationMatchFundingStatus = cmbStatus.SelectedItem.Value
        .GrantApplicationMatchFundingNotes = memFundingNotes.Text
    End With
    dc1.GrantApplicationMatchFundings.InsertOnSubmit(NewGrantApplicationMatchFundings)
    dc1.SubmitChanges()
    'End If


    'dc1.SubmitChanges()

    txtSourceFunding.Text = ""
    spnContributionFunding.Value = ""
    cmbStatus.Value = ""
    memFundingNotes.Text = ""
    grdFunding.DataBind()

     Dim btnTotal = (From o In dc1.Select_GrantApplicationMatchFundingTotal(Session("CurrentProjectID").ToString)).FirstOrDefault
    If Not IsNothing(btnTotal) Then
        spnTotalMatchFunding.Text = btnTotal.TotalFunding
        txtGrantFunding.Text = (spnTotalMatchFunding.Value / spnTotalEstimatedCost.Value) * 100
    Else
        spnTotalMatchFunding.Text = 0
        txtGrantFunding.Text = 0
    End If

    Dim cafChange = (From o In dc1.GrantApplicationCostsAndFundings Where o.GrantApplicationID.ToString = Session("CurrentProjectID").ToString).FirstOrDefault
    If Not IsNothing(cafChange) Then
        cafChange.GrantApplicationID = Session("CurrentProjectID")
        cafChange.GrantApplicationProjectCostsYear1 = spnTotalEstimatedCost.Value
        cafChange.GrantApplicationMatchedFundingNotesYear1 = memNotesMatchFunding.Text
        cafChange.GrantApplicationProjectRequestedYear1 = txtGrantFunding.Value
        dc1.SubmitChanges()
    Else
        Dim NewGrantApplicationCostsAndFundings As New GrantApplicationCostsAndFunding
        With NewGrantApplicationCostsAndFundings
            .GrantApplicationID = Session("CurrentProjectID")
            .GrantApplicationProjectCostsYear1 = spnTotalEstimatedCost.Value
            .GrantApplicationMatchedFundingNotesYear1 = memNotesMatchFunding.Text
            .GrantApplicationProjectRequestedYear1 = txtGrantFunding.Value
        End With
        dc1.GrantApplicationCostsAndFundings.InsertOnSubmit(NewGrantApplicationCostsAndFundings)
        dc1.SubmitChanges()
    End If

结束子

关于sql-server - 在 Visual Studio 2013 中,我只是想使用 VB 将数据传递给 sql 表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30256203/

相关文章:

c# - 保证相同版本的 nuget 包

visual-studio-2012 - 使用 MsBuild 构建 Visual Studio 解决方案时出错

sql-server - 插入后触发 SELECT MIN(COUNT) 插入 ID

c# - 在标记中使用函数返回的服务器控件

php - 如何在php页面的选项列表中显示sql行

c# - 查找字符串中字符的索引

vb.net - 在 vb.net 中将 < 和 > 转换为 < 和 >

c++ - 为什么只执行最后一个线程?

c# - 我怎么知道是否因为外键违规而引发了 SQLexception?

sql-server - 用于 Visual Studio 2010 和 SQL Server 2008 脚本和数据库更新的源代码管理工具?