VB.net中的查询有时不起作用

时间:2016-06-20 10:16:48

标签: sql asp.net vb.net email

我有一个功能可以将信息从webform保存到数据库,这样可以正常工作。

这是我在Onspec.aspx.vb页面中保存功能的代码

   Public Sub SaveClick(ByVal sender As Object, ByVal e As System.EventArgs)

        If Not Page.IsValid Then
            litMessage.Text = ""
            Exit Sub
        End If

        Try

            Dim sName As String = ""

            With EmployerCandidate

                .employerid = Master.Employer.ID
                .employeruserid = 0

                Try
                    .CVID = DocId
                Catch
                End Try

                .Name = uxName.Text
                .SurName = uxSurname.Text

                Try
                    .PostCode = uxPostcode.Text
                Catch ex As Exception
                End Try

                .Email = uxEmail.Text

                Try
                    .MobileNo = uxMobileNo.Text
                Catch ex As Exception
                End Try

                .OnSpec = True

                .OnSpecType = uxOnSpecCategory.SelectedItem.Text
                .Source = uxSourceID.SelectedItem.Text

                .LastEmployer = uxEmployer.Text
                .LastJobTitle = uxJobtitle.Text

                .Profile = uxProfile.Text.Trim

            End With

               ' email()


            ResetForm()

            panForm.Visible = False
            panUpload.Visible = True

            uxSubmit.Visible = False
            uxUpload.Visible = True

            litMessage.Text = "Thank you for submitting your details! We'll be in touch when appropriate vacancy arises. <a href='#' onclick='parent.$.colorbox.close()'>Close</a>"
            UploadPanel.Visible = False


            Response.Redirect("onspec-complete.aspx?e=" & Master.Employer.ID)



        Catch ex As Exception
            litMessage.Text = Core.Helper.FancyMessage("Error as occurred - " & ex.Message.ToString, "ERROR")
        End Try


    End Sub

现在,我想发送电子邮件并将数据保存在数据库中,因此我在ResetForm()之前添加了以下代码行。

Dim specmatch As DataRow = DB.GetRow("SELECT  TOP 1 name,email,password FROM  [user] usr JOIN employeruser empUsr ON usr.id = empUsr.userid WHERE empUsr.empusertype = 1 AND empUsr.employerid = @eid ", DB.SIP("eid", LocalHelper.UserEmployerID))

如果我的查询返回一个值,那么我想通过调用电子邮件功能发送电子邮件。

        If specmatch IsNot Nothing Then
            email()
        End If

但是在这行代码中, employerid 值有时是空的。它无法选择id因此它返回一个空行。因此,它不会调用电子邮件功能。

此ID与

相同
.employerid = Master.Employer.ID

但如果我将LocalHelper.UserEmployerID替换为Master.Employer.ID,则表单会出错:

  

对象引用未设置为对象的实例。

employeeid是一个整数值。

我还尝试在此页面中添加以下代码行(Onspec.aspx.vb)

Public ReadOnly Property EmployerId() As Integer
    Get
        Return Master.Employer.ID
    End Get
End Property

但是我得到了同样的错误:

  

对象引用未设置为对象的实例。

以上代码行没有任何区别。

所以我用Master.Employer.ID

替换了LocalHelper.UserEmployerID

在localhelper.vb文件中(在appcode文件夹下),它使用以下代码行从网站确定employeeid。

Public Shared Function UserEmployerID() As Integer
    If HttpContext.Current.Items("lh_useremployerid") Is Nothing Then
        If LocalHelper.UserTypeCode = "e" Then
            HttpContext.Current.Items("lh_useremployerid") = Core.DB.GetInteger("SELECT employerid FROM employeruser WHERE userid = " & LocalHelper.UserID)
        Else
            HttpContext.Current.Items("lh_useremployerid") = 0
        End If
    End If

    Return HttpContext.Current.Items("lh_useremployerid")
End Function

这些代码行已在很多地方使用,并且工作得很好。在这种情况下,它上周五工作正常,现在不是。没有做出任何改变。

这是我的电子邮件功能

Private Sub email()

    Dim strCandidateName As String = uxName.Text.Trim & " " & uxSurname.Text.Trim
    Dim strCandidateSurname As String = uxSurname.Text
    Dim strCandidateEmail As String = uxEmail.Text.Trim
    Dim strCandidatePhone As String = uxMobileNo.Text.Trim
    Dim strCPass As String = "mpb123"
    Dim strContactType As String = "Speculative Application has been submited on the category of  " & uxOnSpecCategory.SelectedItem.Text
    Dim strContactMessage As String = uxProfile.Text.Trim


    '''''''''''''''''''variable
    Dim qq As String = String.Format("Select top 1 name, email, password FROM [user] WHERE id in (Select userid FROM employeruser WHERE empusertype=1 and employerid= " & LocalHelper.UserEmployerID & ")")

    Dim drow As DataRow = Core.DB.GetRow(qq)

    Dim semail As String = drow.Item("email").ToString
    Dim sname As String = drow.Item("name").ToString
    Dim spass As String = drow.Item("password").ToString


    '''''''''''''''''''variable



    Dim Email As Core.Email

    'New Onspec Candidate Email sent to EmployerUser who is assigned to receive onspec email

    Email = New Core.Email

    With Email
        .ContentID = 99
        .Replacement("ContactName", strCandidateName)
        .Replacement("ContactEmail", strCandidateEmail)
        .Replacement("ContactTelephone", strCandidatePhone)
        .Replacement("ContactType", strContactType)
        .Replacement("ContactMessage", strContactMessage)
        .Replacement("AdminContactName", sname)
        .Replacement("AdminContactEmail", semail)
        .Replacement("SiteName", LocalHelper.AppSetting("sitename"))
        Dim attachments As New Generic.List(Of String)
        If EmployerCandidate.CVID > 0 Then
            Dim doc As New Document(EmployerCandidate.CVID)
            attachments.Add(doc.PathOnDisc)
        End If
        .Attachments = Join(attachments.ToArray(), ";")

        .Send()

    End With



 End Sub

GetRow函数在我的database.vb页面中定义,如下所示,也在许多其他地方使用。

    Shared Function GetRow(ByVal selectQueryText As String, ByVal ParamArray params As SqlParameter()) As DataRow
        Return GetRowFromDB(selectQueryText, CommandType.Text, params)
    End Function

Shared Function GetRowFromDB(ByVal selectCommandText As String, ByVal selectCommandType As CommandType, ByVal ParamArray params As SqlParameter()) As DataRow
        Dim conn As SqlConnection = Nothing
        Try
            conn = GetOpenSqlConnection()

            Return GetRowFromDB(conn, selectCommandText, selectCommandType, params)
        Finally
            If conn IsNot Nothing Then conn.Dispose()
        End Try
    End Function

请建议如何组织我的代码以便它始终有效。

0 个答案:

没有答案