我要做的是點擊GRIDVIEW(代碼加載的內容)中更新功能按鈕 然后更新相關資訊,由于某些原因(說起來太麻煩了),只能使用自己寫的代碼而不是自動生成的UPDATE陳述句。
但是自己寫的UPDATE陳述句出現問題,更新資訊失敗。在SQLrSERVER中能正常使用 所以我猜是呼叫GRIDVIEW中資料時出現問題。但是試過很多方法都不能使用 不知道問題出現在哪里 希望前輩們能幫我看看
下面貼出幾張截圖和代碼
圖一是在SQL中正常使用的代碼
圖二是斷點打完后相關變數為空值
圖三是前端
下面貼出后端代碼(下面截圖可能更好看)
using System;using System.Collections.Generic;using System.Data;using System.Data.SqlClient;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class HTML_ScoreManage : System.Web.UI.Page{ SqlConnection sqlcon; SqlCommand sqlcom; string strCon = "Data Source=DESKTOP-79153UB; Initial Catalog=db_StudentManage;Integrated Security=True"; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bind(); } } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; bind(); } protected void
GridView1_RowUpdating(object sender,
GridViewUpdateEventArgs e) //出現問題的函式
{ //截圖可能看得更清晰
sqlcon = new SqlConnection(strCon);
string sqlstr = "update tb_Choosec001 set SName='" +GridView1.Rows[e.RowIndex].Cells[1].Text.ToString().Trim() + "',Score='" + GridView1.Rows[e.RowIndex].Cells[2].Text.ToString().Trim() + "' where SId='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
//上面幾個GRIDVIEW1表格中資料呼叫為空值
sqlcom = new SqlCommand(sqlstr, sqlcon); sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
GridView1.EditIndex = -1;
bind();
}
//取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; bind(); } public void bind() { string sqlstr = "select * from tb_Choosec001"; sqlcon = new SqlConnection(strCon); SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon); DataSet myds = new DataSet(); sqlcon.Open(); myda.Fill(myds, "tb_Choosec001"); GridView1.DataSource = myds; GridView1.DataKeyNames = new string[] { "SId" };//主鍵 GridView1.DataBind(); sqlcon.Close(); }}


uj5u.com熱心網友回復:
圖二是出現錯誤的方法 應該是update sql陳述句 中呼叫gridview中資料的方法錯誤uj5u.com熱心網友回復:
但是一直想不明白為什么是空值 試過很多方法uj5u.com熱心網友回復:
你前臺有木有加上DataKeyNames="Sid"uj5u.com熱心網友回復:
了 呢后端代碼在bind() 里面加了uj5u.com熱心網友回復:
確保所有的值都取到 然后 update 陳述句無錯誤。即可,然后 指定 datasource 在databind()uj5u.com熱心網友回復:
你好 就是值沒取到 但是我不知道哪里出了問題 能幫我看看 取gridview中表格的值 的陳述句有什么問題嗎
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/130335.html
標籤:ASP.NET
下一篇:WPF程式發布問題
