我想在静态网络方法中访问非静态控件
下面是我的代码,但是在使用datatable对象(dt)绑定gridview时遇到错误,错误是“对象引用没有设置为对象的实例” 问题是我的gridview控件显示空I.E在web方法中不可访问
[System.Web.Services.WebMethod]
public static DataTable GetProductByID(string args)
{
string[] data = args.Trim().Split(',');
try
{
SqlConnection conn;
SqlCommand cmd;
SqlDataAdapter sda;
DataTable dt;
conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
cmd = new SqlCommand();
cmd.CommandText = "SELECT ID, BRANDNAME FROM TBLTBRAND WHERE ID = @productid";
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@brandname", data[1]);
cmd.Parameters.AddWithValue("@productid", Convert.ToInt32(data[0]));
dt = new DataTable();
sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
sda.Fill(dt);
Default obcD = new Default();
obcD.LoadData();
return dt;
**GridView1.DataSource = dt.DefaultView; // error oocupy**
**GridView1.DataBind();**
sda.Dispose();
cmd.Dispose();
conn.Dispose();
}
catch (Exception ex)
{
throw ex;
}
}