asp.net sql 更新报告它正在运行,但没有实际结果
asp.net sql update reports that it's working, but without real results
我有一个很长的具体问题。
我正在创建 asp.net (C#) 网络应用程序。
我要求自定义 DATABASE.cs class, 用于与我在 上的数据库的所有通信真实服务器(不是我正在开发网络应用程序的电脑),它工作正常 - 完全没有错误。
我在 formReports.aspx 中以编程方式创建了一个 table 并且在每一行中,有一些来自数据库的信息,显示在 (asp.net's)TextBox-es,带有按钮 Save 和 Delete。在这个问题中,我感兴趣的是 为什么保存按钮不起作用。
了解一些可能有用的事情:
- formReports.aspx 显示在 iFrame.
的另一个 .aspx 页面中
-保存按钮的点击功能是这样添加的:btn.Click += new EventHandler(Update);
在之前我设置了btn.CommandArgument
一些我需要保存的变量,用"隔开/".
-Update 函数工作正常,获取按钮 CommandArgument
,将它们拆分为变量并调用 DATABASE.cs 函数用这些变量更新。
-取自 CommandArgument
的变量没问题,就像我需要的那样,所以没有错误。
-DATABASE.cs 不是静态的 class.
-在 DATABASE.cs 中,我使用 SqlConnection
和 SqlCommand
.
-我在代码为 运行.
时通过逐步检查检查了所有内容
现在,这是来自 DATABASE.Update 的代码:
public string Update(ReportContent rcOld, string newAccountCodeID, string newAmount)
{
string queryFrom = "FROM [FinReports].[dbo].[ReportContent] ";
string queryWhere =
" WHERE ReportID = '" + rcOld.ReportID +
"' AND AccountCodeID = '" + rcOld.AccountCodeID +
"' AND Amount = '" + rcOld.Amount.ToString().Replace(',', '.') + "'";
//check number of rows
int rowCount;
string query = "SELECT COUNT(*) " + queryFrom + queryWhere;
try { rowCount = int.Parse(Single(query)); }
catch { throw new NotSupportedException(command.CommandText + ""); }
//throw new NotSupportedException(query + "");
//return if eny errors are found
if (rowCount < 1) return "Ne postoji u bazi podataka";
if (rowCount > 1) return "Postoji vise od 1 reda sa istim celijama";
//update row
query = "UPDATE [FinReports].[dbo].[ReportContent] " +
"SET AccountCodeID = '" + newAccountCodeID + "', Amount = " + newAmount.Replace(',', '.') +
queryWhere;
//throw new NotSupportedException(query + "");
//try {
if (NonQuery(query) != 1)
return "Greska";
//} catch { throw new NotSupportedException(query + ""); }
return "";
//}catch{return "Fatalna greska"; }
}
ReportContent.cs 是一个 class,仅用于包含 ReportID、AccountCodeID 和 Amount。当我填充 table 时,在创建 EACH 行时,在 CommandArgument
中,我将来自此 class 的信息放入其中。
btn.CommandArgument
示例:u/1/220/43267,14 其中:
u - 代表 "update"
1 - 代表 table 行号(也是从 DATABASE.cs class.
中预制的获取列表中的列表索引
220 - 代表来自 ReportContent 的 AccountCodeID(来自真实数据库的东西)
43267,14 - 代表来自 ReportContent 的金额(来自真实数据库的东西)
你可能会说,使用 throw new NotSupportedException()
我已经计算出确切的 SQL 查询 ,在真实服务器上尝试过,它工作正常 - 没有错误,并且该行已更新。
函数 Single() 和 NonQuery() 只需打开连接,执行 ExecuteScalar(),即 ExecuteNonQuery(),然后关闭连接 -那里没有错误,它们 return (Single()) 来自单个左上角单元格的字符串,即受影响行的 (NonQuery()) numer。
问题: 我单击 保存按钮 ,函数 更新 来自 formReport.aspx被调用,然后调用DATABASE.Update函数,即returns 空字符串 (""),因为真实数据库中的行被更新了,但是当我检查真实数据库时,这些行根本没有受到影响.
一切运行顺利。我什至补充说,如果 DATABASE.Update returns "",table 行的背景变为黄色 - 如果不是 "",则变为红色(当我单击它时它变为黄色)。
为什么从我(客户端)端得到更新成功的信息,但是当我到达真正的服务器时,刷新结果,我想要的行不受影响?
我在这里回答我自己的问题。
从我在问题中发布的任何内容都看不出问题。
很抱歉大惊小怪,我会给出一个答案,以便其他开发人员可以从我的错误中吸取教训。
当我说我正在以编程方式创建 Save 按钮时,在 CommandArgument 中,我 将当前文本刚刚创建了 TextBox 作为参数 ,因此任何 Update 或 Delete 到数据库都会有一个空字符串作为参数。
现在,我解决了这个问题,方法是为这些文本框创建唯一 ID,将其作为参数发送到 CommandArgument,然后检索该文本框的当前文本。
我创建table的方式
protected void Page_Load(object sender, EventArgs e)
{
//如果(!IsPostBack)
{
如果(会话["user"] == null)
{ Server.Transfer("formMain.aspx"); return; }
if (Request.QueryString["reportID"] == null)
{ //error code here
}
database = new DATABASE();
//load report from db
reportID = int.Parse(Request.QueryString["reportID"]);
List<ReportContent> rows = database.ReportContents(reportID);
//add rows
for (int i = 0; i < rows.Count; i++)
report.Rows.Add(TRow(rows[i].AccountCodeID.ToString(),SetDecimals(rows[i].Amount.ToString()), i));
}
}
因此,不要编写代码
public TableRow TRow(string AccountCodeID, string Amount, int rowNo)
{
TableCell cell = new TableCell();
TableRow row = new TableRow();
Label label = new Label();
string tbACIDID = "tbAccountCodeID" + rowNo;
string tbAID = "tbAmount" + rowNo;
//add row number
label.Text = (rowNo + 1) + ".";
cell.Controls.Add(label);
row.Cells.Add(cell);
//add AccountCodeID
TextBox tbac = new TextBox();
tbac.ID = tbACIDID;
tbac.Text = AccountCodeID;
tbac.Width = 50;
tbac.CssClass = "textbox";
cell = new TableCell();
cell.Controls.Add(tbac);
cell.Attributes.Add("class", "cell");
row.Cells.Add(cell);
//add Amount
tbam = new TextBox();
tbam.ID = tbAID;
tbam.Text = Amount;
tbam.Width = 100;
tbam.CssClass = "textbox";
cell.Controls.Add(tbam);
cell.Attributes.Add("class", "cell");
row.Cells.Add(cell);
//add save button
Button btn = new Button();
btn.Text = "Sacuvaj";
btn.CssClass = "saveButton";
btn.CommandArgument =
"u" + separator +
rowNo + separator +
tbac.Text + separator +
tbam.Text;
btn.Click += new EventHandler(Update);
cell.Controls.Add(btn);
//style cell
cell.Attributes.Add("class", "tableCell");
row.Cells.Add(cell);
//style row
row.Attributes.Add("class", "row");
return row;
}
DO-CODE-FOR-TRow
public TableRow TRow(string AccountCodeID, string Amount, int rowNo)
{
TableCell cell = new TableCell();
TableRow row = new TableRow();
Label label = new Label();
string tbACIDID = "tbAccountCodeID" + rowNo;
string tbAID = "tbAmount" + rowNo;
//add row number
label.Text = (rowNo + 1) + ".";
cell.Controls.Add(label);
row.Cells.Add(cell);
//add AccountCodeID
TextBox tb = new TextBox();
tb.ID = tbACIDID;
tb.Text = AccountCodeID;
tb.Width = 50;
tb.CssClass = "textbox";
cell = new TableCell();
cell.Controls.Add(tb);
cell.Attributes.Add("class", "cell");
row.Cells.Add(cell);
//add Amount
tb = new TextBox();
tb.ID = tbAID;
tb.Text = Amount;
tb.Width = 100;
tb.CssClass = "textbox";
cell.Controls.Add(tb);
cell.Attributes.Add("class", "cell");
row.Cells.Add(cell);
//add save button
Button btn = new Button();
btn.Text = "Sacuvaj";
btn.CssClass = "saveButton";
btn.CommandArgument =
"u" + separator +
rowNo + separator +
tbACIDID + separator +
tbAID;
btn.Click += new EventHandler(Update);
cell.Controls.Add(btn);
//style cell
cell.Attributes.Add("class", "tableCell");
row.Cells.Add(cell);
//style row
row.Attributes.Add("class", "row");
return row;
}
所以,总结一下,这是一个非常愚蠢的错误。但是直到我尝试删除和插入更新的行我才弄明白。我希望这个 "answer" 能节省一些人的时间。
我有一个很长的具体问题。
我正在创建 asp.net (C#) 网络应用程序。
我要求自定义 DATABASE.cs class, 用于与我在 上的数据库的所有通信真实服务器(不是我正在开发网络应用程序的电脑),它工作正常 - 完全没有错误。
我在 formReports.aspx 中以编程方式创建了一个 table 并且在每一行中,有一些来自数据库的信息,显示在 (asp.net's)TextBox-es,带有按钮 Save 和 Delete。在这个问题中,我感兴趣的是 为什么保存按钮不起作用。
了解一些可能有用的事情:
- formReports.aspx 显示在 iFrame.
的另一个 .aspx 页面中
-保存按钮的点击功能是这样添加的:btn.Click += new EventHandler(Update);
在之前我设置了btn.CommandArgument
一些我需要保存的变量,用"隔开/".
-Update 函数工作正常,获取按钮 CommandArgument
,将它们拆分为变量并调用 DATABASE.cs 函数用这些变量更新。
-取自 CommandArgument
的变量没问题,就像我需要的那样,所以没有错误。
-DATABASE.cs 不是静态的 class.
-在 DATABASE.cs 中,我使用 SqlConnection
和 SqlCommand
.
-我在代码为 运行.
现在,这是来自 DATABASE.Update 的代码:
public string Update(ReportContent rcOld, string newAccountCodeID, string newAmount)
{
string queryFrom = "FROM [FinReports].[dbo].[ReportContent] ";
string queryWhere =
" WHERE ReportID = '" + rcOld.ReportID +
"' AND AccountCodeID = '" + rcOld.AccountCodeID +
"' AND Amount = '" + rcOld.Amount.ToString().Replace(',', '.') + "'";
//check number of rows
int rowCount;
string query = "SELECT COUNT(*) " + queryFrom + queryWhere;
try { rowCount = int.Parse(Single(query)); }
catch { throw new NotSupportedException(command.CommandText + ""); }
//throw new NotSupportedException(query + "");
//return if eny errors are found
if (rowCount < 1) return "Ne postoji u bazi podataka";
if (rowCount > 1) return "Postoji vise od 1 reda sa istim celijama";
//update row
query = "UPDATE [FinReports].[dbo].[ReportContent] " +
"SET AccountCodeID = '" + newAccountCodeID + "', Amount = " + newAmount.Replace(',', '.') +
queryWhere;
//throw new NotSupportedException(query + "");
//try {
if (NonQuery(query) != 1)
return "Greska";
//} catch { throw new NotSupportedException(query + ""); }
return "";
//}catch{return "Fatalna greska"; }
}
ReportContent.cs 是一个 class,仅用于包含 ReportID、AccountCodeID 和 Amount。当我填充 table 时,在创建 EACH 行时,在 CommandArgument
中,我将来自此 class 的信息放入其中。
btn.CommandArgument
示例:u/1/220/43267,14 其中:
u - 代表 "update"
1 - 代表 table 行号(也是从 DATABASE.cs class.
中预制的获取列表中的列表索引
220 - 代表来自 ReportContent 的 AccountCodeID(来自真实数据库的东西)
43267,14 - 代表来自 ReportContent 的金额(来自真实数据库的东西)
你可能会说,使用 throw new NotSupportedException()
我已经计算出确切的 SQL 查询 ,在真实服务器上尝试过,它工作正常 - 没有错误,并且该行已更新。
函数 Single() 和 NonQuery() 只需打开连接,执行 ExecuteScalar(),即 ExecuteNonQuery(),然后关闭连接 -那里没有错误,它们 return (Single()) 来自单个左上角单元格的字符串,即受影响行的 (NonQuery()) numer。
问题: 我单击 保存按钮 ,函数 更新 来自 formReport.aspx被调用,然后调用DATABASE.Update函数,即returns 空字符串 (""),因为真实数据库中的行被更新了,但是当我检查真实数据库时,这些行根本没有受到影响.
一切运行顺利。我什至补充说,如果 DATABASE.Update returns "",table 行的背景变为黄色 - 如果不是 "",则变为红色(当我单击它时它变为黄色)。
为什么从我(客户端)端得到更新成功的信息,但是当我到达真正的服务器时,刷新结果,我想要的行不受影响?
我在这里回答我自己的问题。
从我在问题中发布的任何内容都看不出问题。
很抱歉大惊小怪,我会给出一个答案,以便其他开发人员可以从我的错误中吸取教训。
当我说我正在以编程方式创建 Save 按钮时,在 CommandArgument 中,我 将当前文本刚刚创建了 TextBox 作为参数 ,因此任何 Update 或 Delete 到数据库都会有一个空字符串作为参数。
现在,我解决了这个问题,方法是为这些文本框创建唯一 ID,将其作为参数发送到 CommandArgument,然后检索该文本框的当前文本。
我创建table的方式 protected void Page_Load(object sender, EventArgs e) { //如果(!IsPostBack) { 如果(会话["user"] == null) { Server.Transfer("formMain.aspx"); return; }
if (Request.QueryString["reportID"] == null)
{ //error code here
}
database = new DATABASE();
//load report from db
reportID = int.Parse(Request.QueryString["reportID"]);
List<ReportContent> rows = database.ReportContents(reportID);
//add rows
for (int i = 0; i < rows.Count; i++)
report.Rows.Add(TRow(rows[i].AccountCodeID.ToString(),SetDecimals(rows[i].Amount.ToString()), i));
}
}
因此,不要编写代码
public TableRow TRow(string AccountCodeID, string Amount, int rowNo)
{
TableCell cell = new TableCell();
TableRow row = new TableRow();
Label label = new Label();
string tbACIDID = "tbAccountCodeID" + rowNo;
string tbAID = "tbAmount" + rowNo;
//add row number
label.Text = (rowNo + 1) + ".";
cell.Controls.Add(label);
row.Cells.Add(cell);
//add AccountCodeID
TextBox tbac = new TextBox();
tbac.ID = tbACIDID;
tbac.Text = AccountCodeID;
tbac.Width = 50;
tbac.CssClass = "textbox";
cell = new TableCell();
cell.Controls.Add(tbac);
cell.Attributes.Add("class", "cell");
row.Cells.Add(cell);
//add Amount
tbam = new TextBox();
tbam.ID = tbAID;
tbam.Text = Amount;
tbam.Width = 100;
tbam.CssClass = "textbox";
cell.Controls.Add(tbam);
cell.Attributes.Add("class", "cell");
row.Cells.Add(cell);
//add save button
Button btn = new Button();
btn.Text = "Sacuvaj";
btn.CssClass = "saveButton";
btn.CommandArgument =
"u" + separator +
rowNo + separator +
tbac.Text + separator +
tbam.Text;
btn.Click += new EventHandler(Update);
cell.Controls.Add(btn);
//style cell
cell.Attributes.Add("class", "tableCell");
row.Cells.Add(cell);
//style row
row.Attributes.Add("class", "row");
return row;
}
DO-CODE-FOR-TRow
public TableRow TRow(string AccountCodeID, string Amount, int rowNo)
{
TableCell cell = new TableCell();
TableRow row = new TableRow();
Label label = new Label();
string tbACIDID = "tbAccountCodeID" + rowNo;
string tbAID = "tbAmount" + rowNo;
//add row number
label.Text = (rowNo + 1) + ".";
cell.Controls.Add(label);
row.Cells.Add(cell);
//add AccountCodeID
TextBox tb = new TextBox();
tb.ID = tbACIDID;
tb.Text = AccountCodeID;
tb.Width = 50;
tb.CssClass = "textbox";
cell = new TableCell();
cell.Controls.Add(tb);
cell.Attributes.Add("class", "cell");
row.Cells.Add(cell);
//add Amount
tb = new TextBox();
tb.ID = tbAID;
tb.Text = Amount;
tb.Width = 100;
tb.CssClass = "textbox";
cell.Controls.Add(tb);
cell.Attributes.Add("class", "cell");
row.Cells.Add(cell);
//add save button
Button btn = new Button();
btn.Text = "Sacuvaj";
btn.CssClass = "saveButton";
btn.CommandArgument =
"u" + separator +
rowNo + separator +
tbACIDID + separator +
tbAID;
btn.Click += new EventHandler(Update);
cell.Controls.Add(btn);
//style cell
cell.Attributes.Add("class", "tableCell");
row.Cells.Add(cell);
//style row
row.Attributes.Add("class", "row");
return row;
}
所以,总结一下,这是一个非常愚蠢的错误。但是直到我尝试删除和插入更新的行我才弄明白。我希望这个 "answer" 能节省一些人的时间。