VB:列名无效?

时间:2014-05-25 23:33:51

标签: asp.net vb.net

我正在尝试在我拥有的VB Web应用程序上实现搜索功能,并且它抛出一个错误,我无法理解为什么它给我的错误它的建议。

我已按照本指南在网格视图上实现搜索。

http://www.devasp.net/net/articles/display/1425.html

我得到的错误是:异常详细信息:System.Data.SqlClient.SqlException:无效的列名'Sfdc_Account_Owner_ID'

指向此行(32)

 Line 30:         Dim da As New SqlDataAdapter(cmd)
 Line 31:         Dim ds As New DataSet()
 Line 32:         da.Fill(ds)
 Line 33:         conn.Close()
 Line 34:         Return ds

这是背后的代码:

Imports System.Data.SqlClient
Imports System.Data

Public Class WebForm1
Inherits System.Web.UI.Page

Dim cbSelect As Object

Private Property contacted As Object

Private Function CustomersData() As DataSet
    Dim text As String = "SELECT ID, contact,Sales_followUp, case_closed,week,year,
     [account_id], [product_name], [begin_date], [begin_request_date],
     [billing_country_code], [region], [billing_zip], [clear_name], 
     [clear_lower_email], [Territory], [Market Segment] AS column1, [AR Period] AS
     AR_Period, [Revenue Type] AS Revenue_Type, [Service Group] AS Service_Group, 
     [Sfdc Customer Id] AS Sfdc_Customer_Id, 
     [Sfdc Account Owner ID] AS Sfdc_Account_Owner_ID,  
     [Comments] FROM [BusinessSignups]"

    Dim connString As String = "Data Source=xxxxxxxx;Initial Catalog=Support_Metrics;Persist Security Info=True;User ID=xxxxxxxxxx;Password=xxxxxxxx"
    Dim conn As New SqlConnection(connString)
    Dim cmd As New SqlCommand(text, conn)
    conn.Open()
    Dim da As New SqlDataAdapter(cmd)
    Dim ds As New DataSet()
    da.Fill(ds)
    conn.Close()
    Return ds
End Function

Private Function CustomersDataWithSearchText(ByVal field As String, ByVal searchText As String) As DataSet
    Dim text As String = "SELECT ID, [Territory],  [Sfdc Account Owner ID] AS Sfdc_Account_Owner_ID FROM [BusinessSignups] WHERE " & field & " LIKE '%" & searchText & "%'"
    Dim connString As String = "Data Source=xxxxxxxx;Initial Catalog=Support_Metrics;Persist Security Info=True;User ID=xxxxxxxxxx;Password=xxxxxxxx"
    Dim conn As New SqlConnection(connString)
    Dim cmd As New SqlCommand(text, conn)
    conn.Open()
    Dim da As New SqlDataAdapter(cmd)
    Dim ds As New DataSet()
    da.Fill(ds)
    conn.Close()
    Return ds
End Function


Public Sub checkbox_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
    'Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) 'Handles Button3.Click
    For Each gvRow As GridViewRow In GridView1.Rows 'itterate tru all rows
        Dim chkBox As CheckBox = CType(gvRow.FindControl("cbSelect"), CheckBox) 'find the checkBox inside GridView
        Dim chkBox1 As CheckBox = CType(gvRow.FindControl("Sales"), CheckBox) 'find the checkBox inside GridView
        Dim chkBox2 As CheckBox = CType(gvRow.FindControl("Case"), CheckBox) 'find the checkBox inside GridView

        Dim sqlcon As New SqlConnection("Data Source=xxxxxxxx;Initial Catalog=Support_Metrics;Persist Security Info=True;User ID=xxxxxxxxxx;Password=xxxxxxxx")
        Dim sqlComm As New SqlCommand("update BusinessSignups set contact=@cbSelect, comments=@comments, Sales_followUp=@sales, Case_Closed=@Case where ID=@ID", sqlcon) 'this is an insert example, you can do update you can get the current gridView row id using gvRow.Cells(0).Text
        'Dim sqlComm As New SqlCommand("insert into BusinessSignups (contacted) values (@cbSelect)", sqlcon) 'this is an insert example, you can do update you can get the current gridView row id using gvRow.Cells(0).Text
        sqlComm.Parameters.AddWithValue("@comments", gvRow.Cells(10).Text)
        sqlComm.Parameters.AddWithValue("@cbSelect", chkBox.Checked)
        sqlComm.Parameters.AddWithValue("@Sales", chkBox1.Checked) 'passing the @cbSelect parameter to the command
        sqlComm.Parameters.AddWithValue("@Case", chkBox2.Checked)
        sqlComm.Parameters.AddWithValue("@ID", gvRow.Cells(0).Text)
        Using (sqlcon)
            sqlcon.Open() 'open connection
            sqlComm.ExecuteNonQuery() 'execute the command
        End Using
    Next
End Sub

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        GridView1.DataSourceID = ""
        GridView1.DataSource = CustomersData()
        GridView1.DataBind()
    Else
        If ddlSearch.SelectedItem.Text = "Sfdc_Account_Owner_ID" Then
            lblName.Text = "Enter Sfdc_Account_Owner_ID:"
            lblMessage.Text = ""
        Else
            lblName.Text = "Enter Territory:"
            lblMessage.Text = ""
        End If
    End If
End Sub

Protected Sub btnSearch_Click(sender As Object, e As System.EventArgs) Handles btnSearch.Click
    If txtSearch.Text = "" Then
        lblMessage.Text = "Enter " & ddlSearch.SelectedItem.Text & " to search"
        lblMessage.ForeColor = Drawing.Color.Red
        GridView1.DataSourceID = ""
        GridView1.DataSource = CustomersData()
        GridView1.DataBind()
    Else
        lblMessage.Text = ""
        GridView1.DataSourceID = ""
        GridView1.DataSource = CustomersDataWithSearchText(ddlSearch.SelectedItem.Text, txtSearch.Text)
        GridView1.DataBind()
    End If
End Sub

End Class

ASPX级别的一些代码

   <div>
    <asp:Label ID="lblSearch" runat="server" Text="I want to Search"></asp:Label>
       <asp:DropDownList ID="ddlSearch" runat="server" AutoPostBack="true">
       <asp:ListItem>Sfdc_Account_Owner_ID</asp:ListItem>
       <asp:ListItem>Territory</asp:ListItem>
       </asp:DropDownList>

        <asp:Label ID="lblName" runat="server" Text="Enter Contact Name"></asp:Label>
        <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
        <asp:Button ID="btnSearch" runat="server" Text="Search" />
        <asp:Label ID="lblMessage" runat="server"></asp:Label>

  </div>

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

检查sql server表BusinessSignups中的列名Sfdc_Account_Owner_ID,列名不存在或不同。