在ASP.cs中自动填充网格视图

时间:2019-02-12 11:20:02

标签: c# asp.net invalidoperationexception

使用ASP C#VBA单击按钮后,我想在网格视图中显示数据库值,所以我创建了以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _Default: Page {
    protected void Page_Load(object sender, EventArgs e) {
        if (!this.IsPostBack) {
            bindgrid();
        }
    }

    private void show() {}
}

SqlConnection conn;
SqlCommand comm; // SqlDataReader dreader;
string connstring = @"Data Source=INSRVWP-SQL02;Initial Catalog=ADP_IPT;Persist Security Info=True;User ID=ts;Password=0000";

protected void Button1_Click1(object sender, EventArgs e) {
    bin dgrid();
    SqlConnection myconnection = new SqlConnection(connstring);
    myconnection.Open();
    System.Data.DataTable dt = new DataTable("dt");
    DataSet dst = new DataSet();

    int cnt = dst.Tables[0].Rows.Count;
    for (int i = 0; i < cnt; i++) {}
}

private void bindgrid() {
    SqlCommand mycommand = new SqlCommand("insert into (IRIS_Project, Username,Status, MPPI_ID) values('" + TextBox1.Text +

    "', '" + ComboBox1.SelectedValue + "', '" + ComboBox2.SelectedValue + "','" + TextBox2.Text + "')");

    // mycommand.Parameters.AddWithValue(@"dpo.Project");
    using(SqlConnection con = new SqlConnection(connstring)); {
        using(SqlDataAdapter da = new SqlDataAdapter()) {
            mycommand.Connection = conn;
            da.SelectCommand = mycommand;
            using(DataSet ds = new DataSet()) {
                da.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }
    }
}

enter image description here

0 个答案:

没有答案