尝试对我的数据使用网格视图,但输出错误

时间:2015-08-06 09:39:11

标签: vb.net

以下是用于绑定网格的代码,任何人都可以建议我在这个片段中做错了什么

    Dim con As New SqlClient.SqlConnection
    Dim ds As New DataSet
    Dim adapter As SqlDataAdapter
    Dim sql As String
    con.ConnectionString = "Data Source=SHAHRUKH-PC\SQLEXPRESS;Initial Catalog=vb;User ID=sa;Password=sa@1234"
    con.Open()
    sql = "Data Source=SHAHRUKH-PC\SQLEXPRESS;Initial Catalog=vb;User ID=****;Password=*****"
    cmd.CommandText = "select * From demo_vb  Where ID = '" & txtbox4.Text & "'"
    adapter = New SqlClient.SqlDataAdapter
    adapter.Fill(ds)
    GridView1.ItemsSource = New DataView()
    GridView1.DisplayMemberPath = "ID"
    con.Close()

我收到此错误:

  

在调用'Fill'

之前,尚未初始化SelectCommand属性

2 个答案:

答案 0 :(得分:1)

尝试以下代码:

    SqlCommand cmd;
    SqlConnection con = new SqlConnection("Data Source=SUNTECH-PC\\SQLEXPRESS;Initial Catalog=iSense;Integrated Security=True;");
    con.Open();
    cmd = new SqlCommand("Select Picture from Images",con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    GridView1.DataSource = ds;

    GridView1.DataBind();
    con.Close();

答案 1 :(得分:0)

我认为这会有所帮助

        Dim con As New SqlClient.SqlConnection("Data Source=SHAHRUKH-PC\SQLEXPRESS;Initial Catalog=vb;User ID=sa;Password=sa@1234")
        Dim ds As New DataSet
        Dim sql As New SqlCommand("select * From demo_vb  Where ID = '" & txtbox4.Text & "'",con)
        Dim adapter As New SqlDataAdapter(sql)
        con.Open()
        adapter.Fill(ds)
        GridView1.datasource = ds.Tables(0)
        GridView1.databind()
        con.Close()