为什么我们在字符串cid = row.Cells [1] .Text中得到空字符串? [英] why we get empty string in string cid = row.Cells[1].Text;
本文介绍了为什么我们在字符串cid = row.Cells [1] .Text中得到空字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人告诉我如何通过gridview单元格复选框选中事件从数据库读取id,我正在开发一个代码来处理数据库中的0和1值,以批准或不批准用户,但是它在我的计算机中提供了一个空字符串代码为什么,我的代码是
Pls some one tell that how to read id from database by gridview cell check box checked chanded event , i am developing a code to handle 0 and 1 value from database to approove user or not , but it gives a empty string in my code why, my code is
public void chkStatus_OnCheckedChanged(object sender, EventArgs e)
{
CheckBox chkStatus = (CheckBox)sender;
//GridViewRow row = (GridViewRow)chkStatus.NamingContainer;
GridViewRow row = (GridViewRow)chkStatus.NamingContainer;
string cid = row.Cells[1].Text;
bool status = chkStatus.Checked;
//ITWLTD-DEV01
////string constr = @"Server=.\SQLEXPRESS;Database=TestDB;uid=waqas;pwd=sql;";
//string constr = @"Server=ITWLTD-DEV01;Database=property;Integrated Security=true;";
//string query = "UPDATE marketing SET Approved = @Approved WHERE id = @id";
//SqlConnection con = new SqlConnection(constr);
//// SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
//SqlCommand com = new SqlCommand(query, con);
////Convert.ToInt32(eInfo.isActive)
//// Convert.ToBoolean(dataReader["isActive"]);
string constr = @"Server=ITWLTD-DEV01;Database=property;Integrated Security=true;";
string query = "UPDATE market SET Approved = @Approved WHERE id = @id";
SqlConnection con = new SqlConnection(constr);
SqlCommand com = new SqlCommand(query, con);
com.Parameters.Add("@Approved", SqlDbType.Bit).Value = status;
com.Parameters.Add("@id", Convert.ToInt32(SqlDbType.Int)).Value = cid;
//com.Parameters.Add("@id", SqlDbType.Int).Value = cid;
con.Open();
com.ExecuteNonQuery();
con.Close();
// LoadData();
BindData();
}
它给出了空ID,为什么...请有人告诉
it gives empty id why...pls some one tell
推荐答案
尝试修改一下代码,
Hi,
Try modify the code a bit,
CheckBox chkStatus = (CheckBox)sender;
//GridViewRow row = (GridViewRow)chkStatus.NamingContainer;
GridViewRow row = (GridViewRow)chkStatus.NamingContainer;
到
to
CheckBox chkStatus = (CheckBox)sender;
GridViewRow row = (GridViewRow)chkStatus.Parent;
这应该可行.
This should work.
这篇关于为什么我们在字符串cid = row.Cells [1] .Text中得到空字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文