2016-01-07 101 views
1

嗨我已經發布了這個問題,但dint得到答案,請幫助我通過下拉列表顯示gridview。下面是一個沒有必要的,因爲filter有你已經在查詢的末尾添加相同的值在vb.net編輯code.How從asp.net使用vb.net下拉列表中顯示gridview

 Private Sub BindGridView() 
    Dim filter As String = "" 
    If ddlOwnerDepartment.SelectedValue <> "" Then 
     filter = filter & " AND OwnerDepartment ='" & ddlOwnerDepartment.SelectedValue & "'" 
    End If 
    Dim table1 As DataTable = New DataTable("Details") 
    table1.Columns.Add("ReferenceNo") 
    table1.Columns.Add("Title") 
    table1.Columns.Add("Counterparty") 
    table1.Columns.Add("OwnerDepartment") 
    table1.Columns.Add("Status") 
    table1.Columns.Add("CreatedBy") 
    table1.Columns.Add("CreatedOn") 
    table1.Columns.Add("link") 
    table1.Rows.Clear() 
    Dim conSQL As New SqlConnection(conStrDRS) 
    conSQL.Open() 
    Dim cmdSQL As SqlCommand = New SqlCommand("SELECT Title, ReferenceNo,OwnerDepartment,CreatedBy,CreatedOn,Status,Counterparty " & _ 
            " FROM [dbo].[Registration]" & filter) 

    cmdSQL.Connection = conSQL 
    Dim adptSQL As New SqlClient.SqlDataAdapter(cmdSQL) 
    Dim myDataSet As New DataSet() 
    adptSQL.Fill(myDataSet) 
    conSQL.Close() 
    gvDetails.DataSource = myDataSet.Tables(0) 'dvAlert 
    gvDetails.DataBind() 
End Sub   

Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click 
    BindGridView() 
End Sub 
+0

問題是什麼,你所面對?爲什麼你在'OwnerDepartment'上應用兩次過濾器?另外,您應該使用參數化查詢來避免SQL注入攻擊。 –

+0

hello avi是什麼問題?有什麼錯誤?例外請與具體的問題來 – user1140237

+0

當我點擊搜索沒有可用的數據。 – avi

回答

0

代碼你是串聯filtersql query結束。

也不要連接值使用Parameterized SQL。像這樣添加參數到你的sql查詢中。

Dim cmdSQL As SqlCommand = New SqlCommand("SELECT Title, ReferenceNo,OwnerDepartment,CreatedBy,CreatedOn,Status,Counterparty " & 
           " FROM [dbo].[Registration] Where [email protected]"); 
cmdSQL.Paramters.AddWithValue("@OwnerDepartment",ddlOwnerDepartment.SelectedValue); 

編輯

Private Sub BindGridView(bool showAll) 
Dim filter As String = "" 
If ddlOwnerDepartment.SelectedValue <> "" Then 
    filter = filter & " AND OwnerDepartment ='" & ddlOwnerDepartment.SelectedValue & "'" 
End If 
Dim table1 As DataTable = New DataTable("Details") 
table1.Columns.Add("ReferenceNo") 
table1.Columns.Add("Title") 
table1.Columns.Add("Counterparty") 
table1.Columns.Add("OwnerDepartment") 
table1.Columns.Add("Status") 
table1.Columns.Add("CreatedBy") 
table1.Columns.Add("CreatedOn") 
table1.Columns.Add("link") 
table1.Rows.Clear() 
Dim conSQL As New SqlConnection(conStrDRS) 
conSQL.Open() 
Dim cmdSQL As SqlCommand = New SqlCommand("SELECT Title, ReferenceNo,OwnerDepartment,CreatedBy,CreatedOn,Status,Counterparty FROM [dbo].[Registration] "; 

if showAll==false then 
cmdSQL += "Where [email protected]"); 
cmdSQL.Paramters.AddWithValue("@OwnerDepartment",ddlOwnerDepartment.SelectedValue); 
End If 

cmdSQL.Connection = conSQL 
Dim adptSQL As New SqlClient.SqlDataAdapter(cmdSQL) 
Dim myDataSet As New DataSet() 
adptSQL.Fill(myDataSet) 
conSQL.Close() 
gvDetails.DataSource = myDataSet.Tables(0) 'dvAlert 
gvDetails.DataBind() 
End Sub 

Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click 
BindGridView(true) 
End Sub 

現在,當您從dropdown search通從函數調用這個函數以虛假BindGridView(false)

+0

嗨Mairaj它的作品,但我的問題是我想顯示時點擊搜索按鈕,請幫我。感謝您的回答 – avi

+0

IT應該可以檢查您點擊按鈕時是否在'ddlOwnerDepartment.SelectedValue'中獲得了價值? – Mairaj

+0

不,它只顯示從下拉列表中不單擊搜索 – avi

相關問題