日期:2014-05-17  浏览次数:20394 次

关于主从表的问题(2)
CODE:
protected void Page_Load(object sender, EventArgs e)
  {
  if (!Page.IsPostBack)
  {
   
  string SQL = "select c.*,d.fName AS fContractType from tContractGoods a,tContract c,(select * from tContractType) d where a.fID=c.fID and c.fContractType = d.fType";
  GridView1.DataSource = Binding(SQL);
  GridView1.DataKeyNames = new string[] { "fID" };
  GridView1.DataBind();
  }
  }

  protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
  {

  string fID = Convert.ToString(FormView1.SelectedValue);
  string SQL = "select a.*,c.* from tContractGoods a ,tContract c where a.fGoodsType=2 and a.fID='" + fID + "'";
  DetailsView1.DataSource = Binding(SQL);
  DetailsView1.DataBind();
  }
   

  /**/
  /// <summary>
  /// 执行SQL语句返回一个数据表
  /// </summary>
  /// <param name="SQL">所要执行的SQL语句</param>
  /// <returns>DataTable</returns>
  protected DataTable Binding(string SQL)
  {

  SqlConnection myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["hagerconnectionstr"].ConnectionString);
  DataTable dt = new DataTable();
  SqlDataAdapter myAdapter = new SqlDataAdapter(SQL, myConn);
  myAdapter.Fill(dt);
  return dt;

  }



  主表可以显示数据,从表没数据啊  是不是我取不到fID啊 ?
还有string fID = Convert.ToString(FormView1.SelectedValue); 这句是什么意思啊?

------解决方案--------------------
直接设置断点看看拼出来的SQL是什么,还可以去库里直接执行一下,看返回结果
如果SqlDataSource1绑定了FormView1,
则某个记录的ID在FormView1中取:
string id=FormView1.SelectedValue.ToString();
------解决方案--------------------
string fID = Convert.ToString(FormView1.SelectedValue); 
这个是取出formview1的已选定的值