【发布时间】:2012-10-23 15:46:24
【问题描述】:
我有一个奇怪的问题,我似乎无法弄清楚。我为在服务器上运行的商业应用程序创建了一个 Web 组件。如果我将cbxGramEnabled.Checked 更改为True 或False,该函数运行正常并进行更改。但是,如果我离开 cbxGramEnabled.Checked 该函数不会更新数据库。
运行该组件的代码如下所示:
private void WriteToSQL( )
{
WriteEnableGram( _pkKey, cbxGramEnabled.Checked);
}
cbxGramEnabled.Checked 是我的 Web 应用自定义组件上的 CheckBox。
private void WriteEnableGram( int nPKkey, bool bChecked)
{
string szQuery = string.empty;
if (bChecked == true)
szQuery = "UPDATE dbo.CustomKeyAttr SET EnableGram = 1 WHERE pkCustomKeyAttr = " + nPKkey.ToString() + ";";
else
szQuery = "UPDATE dbo.CustomKeyAttr SET EnableGram = 0 WHERE pkCustomKeyAttr = " + nPKkey.ToString() + ";";
try
{
string szConString = @"Data Source=.\sqlexpress;Initial Catalog=MetricDB;User ID=webuser;Password=mypass;";
using (var Conn = new SqlConnection(szConString))
{
Conn.Open();
using (SqlCommand dbQuery = new SqlCommand(szQuery, Conn))
{
dbQuery.ExecuteNonQuery();
}
}
}
catch (SqlException)
{
}
catch (Exception)
{
}
finally
{
cbxEnableGram.Text = szQuery; // at least display what the query is somewhere on the page....
}
}
【问题讨论】:
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
-
什么叫
WriteToSQL?它可能与其他地方的代码有关。 -
WritetoSQL 代码肯定会运行。如果我将函数调用中的 cbxGramEnabled.Checked 更改为 true 或 false,它可以正常工作。
-
放置断点时检查的值是多少?一定是
null,对吧?还会抛出任何 SqlException 或 Exception 吗?我不知道当您尝试运行空查询时会发生什么。最后,view的代码是什么,checkbox实际放在哪里? -
我无权访问运行表单的代码,因此无法剪切和粘贴该代码;对不起。这是该 Web 应用程序中的自定义组件。我知道没有例外,当我在调用函数时转储 true 或 false 时,更新函数的效果很好。我打赌复选框控件为空,即使我在提交表单之前选中了复选框。
标签: c# asp.net checkbox web components