c#
  • asp.net
  • forms
  • gridview
  • 2017-01-10 101 views 1 likes 
    1

    我有一個網格視圖的數據顯示一個問題,當我按下按鈕「agregar」:數據顯示網格視圖

    enter image description here

    它顯示了一個小廣場,並顯示任何數據。

    cn.Open(); 
    MySqlCommand cm = cn.CreateCommand(); 
    cm.CommandType = CommandType.Text; 
    cm.CommandText = "select * from detalle where iddetalle= '" + txt_boleta.Text + "'and idlocal='" + txtlocal.Text + "'"; 
    cm.ExecuteNonQuery(); 
    MySqlDataAdapter da = new MySqlDataAdapter(cm); 
    DataSet ds = new DataSet(); 
    da.Fill(ds,"data"); 
    GridView1.DataSource = ds.Tables["data"]; 
    GridView1.DataBind(); 
    
    +0

    因爲你在沒有執行好,如果執行的查詢將歡迎黑客通過注入 –

    回答

    0

    你必須與你的查詢幾個問題,一個快速的解決辦法是在"'and"'and之間給人一種空間,通過這個你是通過注射開門黑客,所以更好的選擇是使用的參數查詢。幾個建議:

    1. 您正在使用適配器收集查詢結果到DataTable/DataSet中,這樣你就不必在這之前
    2. 執行查詢您使用的是單一的查詢,所以它不是獲取的值這裏需要使用DataSet,然後從Dataset中獲取所需的表,而不是直接使用Adapter將結果表提取到DataTable。
    3. 你可以利用使用塊以及

    總之碼結合網格的應該是這樣的:

    DataTable dsDetalle=new DataTable("Data");   
    using (MySqlCommand commandSql = cn.CreateCommand()) 
    { 
        commandSql.CommandType = CommandType.Text; 
        commandSql.CommandText = "select * from detalle where [email protected] and [email protected]"; 
        commandSql.Parameters.AddWithValue("@iddetalle", "txt_boleta.Text"); 
        commandSql.Parameters.AddWithValue("@idlocal", "txtlocal.Text"); 
        MySqlDataAdapter sqlAdapter = new MySqlDataAdapter(commandSql); 
        sqlAdapter.Fill(dsDetalle); 
    } 
    GridView1.DataSource = dsDetalle; 
    GridView1.DataBind(); 
    
    +0

    感謝您的回答,但是當我複製代碼出現下一個錯誤 sqlAdapter.Fill(dsDetalle,「data」); 錯誤\t \t 2的La MEJOR coincidencia德方法方法sobrecargado第 'System.Data.Common.DataAdapter.Fill(System.Data.DataTable,System.Data.IDataReader)' tiene algunos argumentos沒有válidos\t C:\用戶\ Hardfix1 \文檔\ Visual Studio的 –

    +0

    感謝 但是當更新的代碼 的IDE顯示下一個錯誤 錯誤 USO德拉局部變量沒有asignada「dsDetalle」 對不起我的無知,但即時通訊在programation –

    +0

    @WladimirArnaldoBriceoMirand新:謝謝對於嘗試和迴應,我已在帖子中進行了更新,請您嘗試更改嗎? –

    相關問題