我正在尝试使用以下代码在 GridView 上获取值:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
string Username = row.Cells[3].Text;
string Password = row.Cells[4].Text;
string Email = row.Cells[5].Text;
string ID_Inscricao = row.Cells[1].Text;
SqlConnection sqlConn = new SqlConnection(
ConfigurationManager.ConnectionStrings["FormacaoConnectionString"].ToString());
SqlCommand sqlComm = new SqlCommand();
sqlComm = sqlConn.CreateCommand();
sqlComm.Parameters.Add("@Username", SqlDbType.Text);
sqlComm.Parameters.Add("@Password", SqlDbType.Text);
sqlComm.Parameters.Add("@Email", SqlDbType.Text);
sqlComm.Parameters.Add("@ID_Inscricao", SqlDbType.Text);
sqlComm.Parameters["@Username"].Value = Username;
sqlComm.Parameters["@Password"].Value = Password;
sqlComm.Parameters["@Email"].Value = Email;
sqlComm.Parameters["@ID_Inscricao"].Value = ID_Inscricao;
string sql = "INSERT INTO Utilizadores " +
"(Username, Password, Email,ID_Inscricao) " +
"VALUES (@Username, @Password, @Email, @ID_Inscricao)";
sqlComm.CommandText = sql;
sqlConn.Open();
sqlComm.ExecuteNonQuery();
sqlConn.Close();
}
所以,问题是我无法从 GridView 中获取值,而是得到“NullReferenceException 未被用户代码处理”。谁能告诉我我做错了什么?
最好的祝福