“在调用'Fill'之前,尚未初始化SelectCommand属性。”

时间:2015-12-16 12:11:38

标签: c# asp.net

public partial class Address : System.Web.UI.Page
{

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

   string query;


    protected void Page_Load(object sender, EventArgs e)
    {


           con.Open();
           query = "select [Addid] from Address";
           int i=1;
           SqlCommand cmd = new SqlCommand(query, con);
           SqlDataAdapter da = new SqlDataAdapter();
           DataSet ds = new DataSet();
           DataTable dtab = new DataTable();
          da.Fill(ds);
          txtaddid.Text = ds.Tables[0].Rows[0][0].ToString();

          if (ds.Tables[0].Rows.Count == 0)
          txtaddid.Text = "Add" + i;

       else 
              if (ds.Tables[0].Rows.Count > 0)

       {

            int id = ds.Tables[0].Rows.Count + 1;
             txtaddid.Text = "Add" + id;
        }

  con.Close();

       }

我想要做的是在页面加载时从地址表中的addid列中检索数据到文本框

1 个答案:

答案 0 :(得分:2)

SqlDataAdapter da = new SqlDataAdapter();

更改为

SqlDataAdapter da = new SqlDataAdapter(cmd);