在web方法asp.net中访问非静态控件

时间:2013-12-04 10:27:47

标签: c# asp.net gridview

我想在静态网络方法中访问非静态控件

下面是我的代码,但是在使用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;
        }
    }

0 个答案:

没有答案