简体   繁体   English

在网格视图中显示动态数据

[英]Displaying dynamic data in a Grid View

I want to display data in a grid view by dynamically inserting data from textboxes in a database , on click of a button 我想通过单击按钮从数据库中的文本框中动态插入数据,从而在网格视图中显示数据

protected void Button1_Click(object sender, EventArgs e)
    {
        //string Name = TextBox1.Text;
        //string Summary = TextBox2.Text;


        //Inserts the FirstName variable into the db-table
        SqlConnection conn = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True");
        conn.Open();
        SqlCommand MyCommand = new SqlCommand("INSERT INTO Table2 (Name,Summary) Values ('" + TextBox1.Text + "' , '" + TextBox2.Text + "');",conn);
        MyCommand.ExecuteNonQuery();
        using (SqlCommand cmd = conn.CreateCommand())
        {
            SqlCommand com = new SqlCommand("Select * from Table2", conn);
            SqlDataAdapter sda = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            //GridView1.DataSource = ds;
            // GridView1.DataBind();
        }
        conn.Close();
        conn.Dispose();
    }

I am not getting any error while running this code but it is not showing any data in the grid view 运行此代码时没有出现任何错误,但是在网格视图中未显示任何数据

You have to give datasource as a datatable instead of dataset. 您必须将数据源作为数据表而不是数据集。

eg: 例如:

using (SqlCommand cmd = conn.CreateCommand())
        {
            SqlCommand com = new SqlCommand("Select * from Table2", conn);
            SqlDataAdapter sda = new SqlDataAdapter(com);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

Apart from uncommenting those lines of code you need to set the Datasource to the datatable that has been populated not the DataSet... 除了取消注释这些代码行之外,您还需要将数据源设置为已填充而不是数据集的数据表...

dataGridView1.DataSource = ds.Tables[0];

On a separate note your initial query is vulnerable to SQL Injection attacks - you should never concatenate user input to form SQL queries, use Parameterised Queries instead eg 另外请注意,您的初始查询容易受到SQL注入攻击-您不应将用户输入连接起来以形成SQL查询,而应使用参数化查询,例如

    SqlCommand MyCommand = new SqlCommand("INSERT INTO Table2 (Name,Summary) Values (@tb1, @tb2);", conn);
    MyCommand.Parameters.AddWithValue("@tb1", TextBox1.Text);
    MyCommand.Parameters.AddWithValue(@"tb2", TextBox2.Text);
    MyCommand.ExecuteNonQuery();

You are also using a using statement with a sqlcommand that you never use 您还使用了从未使用过的sqlcommand的using语句

 using (SqlCommand cmd = conn.CreateCommand())

should really be 应该真的是

using (SqlCommand com = new SqlCommand("Select * from Table2", conn))

You need to uncomment the code where the GridView is getting bind to the data source, as below. 您需要取消注释GridView绑定到数据源的代码,如下所示。

using (SqlCommand cmd = conn.CreateCommand())
        {
            SqlCommand com = new SqlCommand("Select * from Table2", conn);
            SqlDataAdapter sda = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }

Aliter: Create a DataTable instead of a DataSet and fill it using the DataAdapter . Aliter:创建一个DataTable而不是DataSet并使用DataAdapter填充它。

DataTable dt=new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();

Try it and let us know if this works for you. 试试看,让我们知道这是否适合您。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM