为什么要使用两个变量会导致我的查询,没有行返回 [英] Why using two variable causes my query to return with no row
问题描述
我有对每行一个按钮,中继器创建以下asp.net页面:
I have the following asp.net page which has a button for each row that the repeater creates:
<asp:Repeater runat="server" ID="rptContent" OnItemCommand="btnGeneratePDF_Click">
<HeaderTemplate>
<table border="0" style="width: 95%;">
<tr>
<td style="width: 25%;">Name</td>
<td style="width: 25%;">Last Four SSN #</td>
<td style="width: 25%;">PDF Generator</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("name").ToString() %></td>
<td><%# Eval("ssn3").ToString() %></td>
<td><asp:Button ID="btnGeneratePDF" runat="server" Text="Generate PDF" CommandArgument='<%# Eval("name").ToString() + ", " + Eval("ssn3").ToString() %>' /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
我的code的背后是这样的:
My code behind looks like this:
protected void btnGeneratePDF_Click(object sender, CommandEventArgs e)
{
string[] ar = e.CommandArgument.ToString().Split(',');
this.writeData(ar[0], ar[1]);
}
public void writeData(string k, string c)
{
Conn = new SqlConnection(cString);
Conn.Open();
//MessageBox.Show(k);
//MessageBox.Show(c);
nameE = txtName.Text;
var pdfPath = Path.Combine(Server.MapPath("~/PDFTemplates/fw9.pdf"));
// Get the form fields for this PDF and fill them in!
var formFieldMap = PDFHelper.GetFormFieldNames(pdfPath);
formFieldMap["topmostSubform[0].Page1[0].f1_01_0_[0]"] = k;
//sqlCode = "SELECT * FROM [db].[dbo].[TablePDFTest] WHERE [name] = '" + nameE + "'";
sqlCode = "SELECT * FROM [db].[dbo].[TablePDFTest] WHERE [name] = '" + k + "' AND [ssn3] = '" + c + "'";
//MessageBox.Show("" + sqlCode.ToString());
using (SqlCommand command = new SqlCommand(sqlCode, Conn))
{
command.CommandType = CommandType.Text;
using (reader = command.ExecuteReader())
{
if (reader.HasRows)
{
if (reader.Read())
{
formFieldMap["topmostSubform[0].Page1[0].f1_02_0_[0]"] = reader.GetValue(1).ToString();
formFieldMap["topmostSubform[0].Page1[0].f1_04_0_[0]"] = reader.GetValue(2).ToString();
formFieldMap["topmostSubform[0].Page1[0].f1_05_0_[0]"] = reader.GetValue(3).ToString();
formFieldMap["topmostSubform[0].Page1[0].f1_07_0_[0]"] = reader.GetValue(4).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField1[0]"] = reader.GetValue(5).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[0]"] = reader.GetValue(6).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[1]"] = reader.GetValue(7).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[2]"] = reader.GetValue(8).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[3]"] = reader.GetValue(9).ToString();
}
}
}
}
// Requester's name and address (hard-coded)
formFieldMap["topmostSubform[0].Page1[0].f1_06_0_[0]"] = "Medical Group\n27 West Ave\nPurchase, NY 10577";
var pdfContents = PDFHelper.GeneratePDF(pdfPath, formFieldMap);
PDFHelper.ReturnPDF(pdfContents, "Completed-W9.pdf");
}
如果我
SQL code
是 SQL code =SELECT * FROM [DSPCONTENT01]。[DBO]。[TablePDFTest] WHERE [名] ='+ K +'; //和[ssn3] ='+ C +';
它工作正常,为 formFieldMap
但如果我的
SQL code
是 SQL code =SELECT * FROM [DSPCONTENT01]。[DBO]。[TablePDFTest] WHERE [名] ='+ K +'AND [ssn3] ='+ C +';
的 formFieldMap
不正常工作。
这是中继器显示什么的例子:
This is an example of what the repeater displays:
我怎样才能解决这个问题?
How can I fix it?
更新
我与的MessageBox
使用同时安装了可变查询显示值测试:
I did a test with MessageBox
to display the value using the query with both variable:
public void writeData(string k, string c)
{
Conn = new SqlConnection(cString);
Conn.Open();
//MessageBox.Show(k);
//MessageBox.Show(c);
nameE = txtName.Text;
var pdfPath = Path.Combine(Server.MapPath("~/PDFTemplates/fw9.pdf"));
// Get the form fields for this PDF and fill them in!
var formFieldMap = PDFHelper.GetFormFieldNames(pdfPath);
formFieldMap["topmostSubform[0].Page1[0].f1_01_0_[0]"] = k;
sqlCode = "SELECT * FROM [db].[dbo].[TablePDFTest] WHERE [name] = '" + k + "' AND [ssn3] = '" + c + "'";
//MessageBox.Show("" + sqlCode.ToString());
using (SqlCommand command = new SqlCommand(sqlCode, Conn))
{
command.CommandType = CommandType.Text;
using (reader = command.ExecuteReader())
{
if (reader.HasRows)
{
if (reader.Read())
{
MessageBox.Show(reader.GetValue(1).ToString());
MessageBox.Show(reader.GetValue(2).ToString());
MessageBox.Show(reader.GetValue(3).ToString());
MessageBox.Show(reader.GetValue(4).ToString());
MessageBox.Show(reader.GetValue(5).ToString());
MessageBox.Show(reader.GetValue(6).ToString());
MessageBox.Show(reader.GetValue(7).ToString());
MessageBox.Show(reader.GetValue(8).ToString());
MessageBox.Show(reader.GetValue(9).ToString());
/*formFieldMap["topmostSubform[0].Page1[0].f1_02_0_[0]"] = reader.GetValue(1).ToString();
formFieldMap["topmostSubform[0].Page1[0].f1_04_0_[0]"] = reader.GetValue(2).ToString();
formFieldMap["topmostSubform[0].Page1[0].f1_05_0_[0]"] = reader.GetValue(3).ToString();
formFieldMap["topmostSubform[0].Page1[0].f1_07_0_[0]"] = reader.GetValue(4).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField1[0]"] = reader.GetValue(5).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[0]"] = reader.GetValue(6).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[1]"] = reader.GetValue(7).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[2]"] = reader.GetValue(8).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[3]"] = reader.GetValue(9).ToString();*/
}
}
}
}
// Requester's name and address (hard-coded)
/*formFieldMap["topmostSubform[0].Page1[0].f1_06_0_[0]"] = "Medical Group\n27 West Ave\nPurchase, NY 10577";
var pdfContents = PDFHelper.GeneratePDF(pdfPath, formFieldMap);
PDFHelper.ReturnPDF(pdfContents, "Completed-W9.pdf");*/
}
当我点击按钮的消息框不显示了,而不是,什么都不会发生。
The messagebox isn't displayed anymore instead when I click on the button, nothing happens.
推荐答案
什么是在SQL ssn3
的数据类型?你逝去的 C
作为一个字符串。这是值得关注一下。也许只是尝试删除你的周围值单引号C(如果它是例如整数)。
What is the data type of ssn3
in SQL? You are passing c
as a string literal. That is something to look at. Maybe just try removing the single quotes around your value for c (if it is integer for instance).
此外,当您键入查询到一个查询分析器与WHERE子句一些测试数据,会发生什么?你知道应该有两个匹配值的行?
Also, what happens when you type the query into a query analyzer with some test data for the WHERE clause? Do you know if there SHOULD be rows matching both values?
这篇关于为什么要使用两个变量会导致我的查询,没有行返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!