C# 在ASP.cs中自动填充网格视图
我想在使用ASP C#VBA单击按钮后在网格视图中显示我的数据库值,因此我创建了以下代码:C# 在ASP.cs中自动填充网格视图,c#,asp.net,invalidoperationexception,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 c
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();
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Data.SqlClient;
使用系统数据;
公共部分类\u默认值:第页{
受保护的无效页面加载(对象发送方、事件参数e){
如果(!this.IsPostBack){
bindgrid();
}
}
私有void show(){}
}
SqlConnection-conn;
SqlCommand comm;//SqlDataReader恐惧者;
字符串connstring=@“数据源=INSRVWP-SQL02;初始目录=ADP_IPT;持久安全信息=True;用户ID=ts;密码=0000”;
受保护的无效按钮1\u单击1(对象发送者,事件参数e){
bin dgrid();
SqlConnection myconnection=新的SqlConnection(connstring);
myconnection.Open();
System.Data.DataTable dt=新数据表(“dt”);
数据集dst=新数据集();
int cnt=dst.Tables[0].Rows.Count;
对于(int i=0;i
您没有在命令上传递连接对象,请使用like
SqlCommand=newsqlcommand(queryString,connection)
您没有在命令上传递连接对象,请使用likeSqlCommand=newsqlcommand(queryString,connection)代码>