当前位置: 代码迷 >> ASP.NET >> gridview后加一个button,点击button将当前行的数据写入session保存的登录名所在行,但是小弟我的代码有异常,help!
  详细解决方案

gridview后加一个button,点击button将当前行的数据写入session保存的登录名所在行,但是小弟我的代码有异常,help!

热度:7451   发布时间:2013-02-25 00:00:00.0
gridview后加一个button,点击button将当前行的数据写入session保存的登录名所在行,但是我的代码有错误,help!!!
[code=C#][/code] <asp:ButtonField ButtonType="Button" CommandName="second" Text="第二志愿" >
  <ItemStyle Wrap="False" />
  </asp:ButtonField>

后台代码[code=C#][/code] protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
  {
  if (e.CommandName == "first")
  {

  int i = Int32.Parse((string)e.CommandArgument);
  string s = GridView1.Rows[i].Cells[0].Text;
  string strconn = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\毕业设计管理系统1\App_Data\database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
  SqlConnection conn = new SqlConnection(strconn);
  conn.Open();
  string sql = @"select * from [assignment] where assignmentname='" + s + "'";
  SqlCommand strcom = new SqlCommand(sql, conn);
  SqlDataReader sqlread = strcom.ExecuteReader();

  string strconn1 = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\毕业设计管理系统1\App_Data\database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
  SqlConnection conn1 = new SqlConnection(strconn1);
  conn1.Open();
  while (sqlread.Read())
  {
  string strsql1 = @"INSERT INTO [diyizhiyuan] (assignmentid, assignmentname,assignment_teacher,assignment_source,assignment_leibie,assignment_neirong,assignment_require,
  assignment_chengguo,xueyuan,tzhiwu,assignment_xnum, assignment_snum,tid,time)
  VALUES('" + sqlread[0].ToString() + "','" + sqlread[1].ToString() + "','" + sqlread[2].ToString() + "','" + sqlread[3].ToString() + "','" + sqlread[4].ToString() + "','" + sqlread[5].ToString() + "','" + sqlread[6].ToString() + "','" + sqlread[7].ToString() + "','" + sqlread[8].ToString() + "','" + sqlread[9].ToString() + "','" + sqlread[10].ToString() + "','" + sqlread[11].ToString() + "','" + sqlread[12].ToString() + "','" + sqlread[13].ToString() + "')";
  //sqlcmd1.CommandText = strsql1;
  SqlCommand sqlcom1 = new SqlCommand(strsql1, conn1);
  sqlcom1.ExecuteNonQuery();
  }
  conn1.Close();
  conn.Close();


   


  }

  else if (e.CommandName == "second")
  {
  int j = Int32.Parse((string)e.CommandArgument);
  string s = GridView1.Rows[j].Cells[0].Text;
  string strconn = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\毕业设计管理系统1\App_Data\database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
  SqlConnection conn = new SqlConnection(strconn);
  conn.Open();
  string sql = @"select * from [assignment] where assignmentname='" + s + "'";
  SqlCommand strcom = new SqlCommand(sql, conn);
  SqlDataReader sqlread = strcom.ExecuteReader();
  Label1.Text = "<script language=javascript>alert('确定该课题审核未通过?');</script>";
  string strconn1 = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\毕业设计管理系统1\App_Data\database.mdf;Integrated Security=True;User Instance=True";
  SqlConnection conn1 = new SqlConnection(strconn1);
  conn1.Open();
  相关解决方案