2014-01-12 82 views
1

我是ASP.NET新手,併爲salesdepartment構建一個小動態網站註冊他們的salescompetions銷售。錯誤:類型'System.Data.SqlClient.SqlException'類型的異常發生在System.Data.dll中,但未在用戶代碼中處理

我有一個頁面,在一個登錄後,由一對組合框/下拉列表組成,並在'按鈕'按鈕上我想用數據庫中的所有選定數據觸發一個新記錄。一切似乎去罰款,第二,但最終出現以下錯誤信息:

類型「System.Data.SqlClient.SqlException」出現在system.data.dll但在用戶代碼中沒有處理的例外 附加信息:無效的列名稱'KunderID'。 無效的列名稱'KundeTypeID'。 列名'MachineModellID'無效。 列名'AntallID'無效。 無效的列名稱'BrukerID'。

它指向下面部分(該線起始MBExec =)在DBConnection.vb文件:

Public Shared Function MBExec(ByVal SQL As String) As String 
    Dim cmd As New SqlCommand(SQL, MBConn) 
    MBExec = Convert.ToString(cmd.ExecuteScalar()) 
    cmd.Connection.Close() 
End Function  

在源代碼OG相關頁面的它的相關部分是以下的(底符合MBExec開始),由此我不能看到columnsnames是錯誤的:

Protected Sub RegisterSale(sender As Object, e As EventArgs) 

    Dim KundeNavn As DropDownList = DropDownListKundeNavn 
    Dim TypeKunde As DropDownList = DropDownListTypeKunde 
    Dim MachineModell As DropDownList = DropDownListMachineModell 
    Dim Antall As DropDownList = DropDownListAntall 
    Dim Bruker As DropDownList = DropDownListBruker 


    If KundeNavn.SelectedItem.Text = "Velg" Then 
     Dim msg = "Select or add a new customer" 
     Dim msgTittle = "Missing Customer Name" 
     MsgBox(msg, MsgBoxStyle.Critical, msgTittle) 
     Exit Sub 
    Else 
     Dim msg1 = "Are you sure to continue?" 
     Dim title = "Confirm Sale Registration" 
     Dim style = MsgBoxStyle.YesNo 
     Dim responce = MsgBox(msg1, style, title) 
     If responce = MsgBoxResult.Yes Then 
      Dim msg = "Thank you for your efforts, you are closer to becoming a sales champion!" 
      Dim msgTittle = "Your Sale has been recorded" 
      MsgBox(msg, MsgBoxStyle.Information, msgTittle) 

      'Varibles to hold the DataValueField from the dropboxes 
      Dim KundeID As Integer 
      Dim TypeKundeID As Integer 
      Dim MachineModellID As Integer 
      Dim AntallID As Integer 
      Dim BrukerID As Integer 

      'Converts the DataValueField to an Integer 

      KundeID = Convert.ToInt32(KundeNavn.SelectedValue.ToString()) 
      TypeKundeID = Convert.ToInt32(TypeKunde.SelectedValue.ToString()) 
      MachineModellID = Convert.ToInt32(MachineModell.SelectedValue.ToString()) 
      AntallID = Convert.ToInt32(Antall.SelectedValue.ToString()) 
      BrukerID = Convert.ToInt32(Bruker.SelectedValue.ToString()) 

      MBExec("INSERT INTO KyoceraSalgReg(KunderID, KundeTypeID, MachineModellID, AntallID, BrukerID) Values (KunderID, KundeTypeID, MachineModellID, AntallID, BrukerID)") 

      Exit Sub 
     Else 
      Exit Sub 
     End If 
    End If 

End Sub  

我將非常感謝,如果有人可以幫助我在朝着正確的方向。如果我理解正確,不知何故列名不被識別,我只是不明白爲什麼。

乾杯:)

更新1:

MBExec看起來是這樣的:

Public Shared Function MBExec(ByVal SQL As String) As String 
    Dim cmd As New SqlCommand(SQL, MBConn) 
    MBExec = Convert.ToString(cmd.ExecuteScalar()) 
    cmd.Connection.Close() 
End Function  

而且KunderID數據類型爲字符串,選擇從一個DropDownList

+1

您正在使用您嘗試相同的變量插入並告訴sql要插入哪些列,這與列名是不一樣的。 – OneFineDay

+0

嗨,謝謝。我擺脫了無效列名稱錯誤,但仍然缺少一件事。猜測它與你關於AddWithValue的答案的第二部分有關。我閱讀鏈接,嘗試了不同的東西,但似乎沒有任何工作。我真的不明白那部分:( – tnuis

+0

'在System.Data.dll中發生類型'System.Data.SqlClient.SqlException'的異常,但未在用戶代碼中處理 附加信息:必須聲明標量變量「@KunderID 「。' – tnuis

回答

0
Protected Sub RegisterSale(sender As Object, e As EventArgs) 

    Dim KundeNavn As DropDownList = DropDownListKundeNavn 
    Dim TypeKunde As DropDownList = DropDownListTypeKunde 
    Dim MachineModell As DropDownList = DropDownListMachineModell 
    Dim Antall As DropDownList = DropDownListAntall 
    Dim Bruker As DropDownList = DropDownListBruker 

    'Varibles to hold the DataValueField from the dropboxes 
    Dim KunderID As Integer = Convert.ToInt32(KundeNavn.SelectedValue.ToString()) 
    Dim TypeKundeID As Integer = Convert.ToInt32(TypeKunde.SelectedValue.ToString()) 
    Dim MachineModellID As Integer = Convert.ToInt32(MachineModell.SelectedValue.ToString()) 
    Dim AntallID As Integer = Convert.ToInt32(Antall.SelectedValue.ToString()) 
    Dim BrukerID As Integer = Convert.ToInt32(Bruker.SelectedValue.ToString()) 


    'Sets the Selected values from dropdownlist 
    Dim ParamKunderID = New SqlParameter() 
    ParamKunderID.ParameterName = "@KunderID" 
    ParamKunderID.Value = KunderID 

    Dim ParamTypeID = New SqlParameter 
    ParamTypeID.ParameterName = "@KundeTypeID" 
    ParamTypeID.Value = TypeKundeID 

    Dim ParamMachineModellID = New SqlParameter() 
    ParamMachineModellID.ParameterName = "@MachineModellID" 
    ParamMachineModellID.Value = MachineModellID 

    Dim ParamAntallID = New SqlParameter 
    ParamAntallID.ParameterName = "@AntallID" 
    ParamAntallID.Value = AntallID 

    Dim ParamBrukerID = New SqlParameter 
    ParamBrukerID.ParameterName = "@BrukerID" 
    ParamBrukerID.Value = BrukerID 

    If KundeNavn.SelectedItem.Text = "Velg" Then 
     Dim msg = "Velg eller legge til en ny kunde" 
     Dim msgTittle = "Mangler Kundenavn" 
     MsgBox(msg, MsgBoxStyle.Critical, msgTittle) 
     Exit Sub 
    Else 
     Dim msg1 = "Er du sikker på at du vil fortsette?" 
     Dim title = "Bekrefte salg registrering" 
     Dim style = MsgBoxStyle.YesNo 
     Dim responce = MsgBox(msg1, style, title) 

     If responce = MsgBoxResult.Yes Then 
      MBExec("INSERT INTO KyoceraSalgReg(KunderID, KundeTypeID, MachineModellID, AntallID, BrukerID)" & " Values " & "(" & KunderID & "," & TypeKundeID & "," & MachineModellID & "," & AntallID & "," & BrukerID & ")") 
      Dim msg = "Takk for din innsats, du er nærmere å bli et Salg Mester!" 
      Dim msgtittle = "Din salget er registrert" 
      MsgBox(msg, MsgBoxStyle.Information, msgtittle) 

     End If 


    End If 
End Sub 
+0

嗨DonA和Austine,非常感謝。 Austines的答案是正確的金錢,並做了訣竅:) – tnuis

2

做出試試這個辦法:

MBExec("INSERT INTO KyoceraSalgReg(KunderID, KundeTypeID, MachineModellID, AntallID, BrukerID) Values (@KunderID, @KundeTypeID, @MachineModellID, @AntallID, @BrukerID)") 

使用參數化查詢來添加值:

cmd.Parameter.AddWithValue("@KunderID", KunderID) 

AddWithValue

您可能需要作出的SqlParameter的不同實例 - Example

相關問題