在gridview我使用复选框 [英] In gridview i am using check box
问题描述
我正在使用网格视图执行应用程序。在数据库中为复选框我使用位数据类型。
数据库字段中的复选框如下;
列名数据类型长度allownulls
激活位1 1
in网格视图代码的来源如下;
i am doing the application using grid view.in the database for check box i am using bit datatype.
for the check box in the database field as follows;
Columnname datatype length allownulls
Activate bit 1 1
in the source for the grid view code as follows;
<asp:TemplateField HeaderText=Activate SortExpression="Activate">
<itemtemplate>
<asp:CheckBox ID="chkStatus" runat="server"
AutoPostBack="true" OnCheckedChanged="chkStatus_OnCheckedChanged"
Checked='<%# Convert.ToBoolean(Eval("Activate")) %>'
Text='<%# Eval("Activate").ToString().Equals("True") ? " Activate " : " Not Activate " %>' />
</itemtemplate>
在aspx页面中,复选框代码如下;
In the aspx page for check box code as follows;
public void chkStatus_OnCheckedChanged(object sender, EventArgs e)
{
CheckBox chkStatus = (CheckBox)sender;
GridViewRow row = (GridViewRow)chkStatus.NamingContainer;
string vid = row.Cells[1].Text;
bool status = chkStatus.Checked;
string query = "UPDATE Vendors SET Activate = @Activate WHERE VendorId = @VendorId";
SqlConnection conn = new SqlConnection("Server=(local);initial catalog=master;Trusted_Connection=True");
SqlCommand com = new SqlCommand(query, conn);
com.Parameters.Add("@Activate", SqlDbType.Bit).Value = status;
com.Parameters.Add("@VendorId", SqlDbType.Bit).Value = vid;
conn.Open();
com.ExecuteNonQuery();
conn.Close();
}
当我运行网格视图时,它显示错误如下
无法将对象从DBNull强制转换为其他类型。
来自我在aspx和源页面的编码什么是错误请帮助我。
when i run the grid view it shows the error as follows
Object cannot be cast from DBNull to other types.
from my coding in the aspx and source page what is error please help me.
推荐答案
你可以通过以下链接遇到类似的问题。
http://stackoverflow.com/questions/1979806/using-evalitem-handling-null-value-and-showing-0-aginst [ ^ ]
http://stackoverflow.com/questions/536212/how-do-i-handle-conversion-from-type-dbnull-to-type-string-is-not-有效 [ ^ ]
You can go through the below links who faced similar issue.
http://stackoverflow.com/questions/1979806/using-evalitem-handling-null-value-and-showing-0-aginst[^]
http://stackoverflow.com/questions/536212/how-do-i-handle-conversion-from-type-dbnull-to-type-string-is-not-valid[^]
这篇关于在gridview我使用复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!