将参数发送到另一个页面时出错 [英] Error in send parameter to another page
本文介绍了将参数发送到另一个页面时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
抱歉,我说英语不多
i有两个页面名称finder.aspx和defualt2.aspx
i am使用下面的代码,我发送将参数发送到default2.aspx以便在数据库中搜索
StringBuilder s = new StringBuilder();
List< string> i = new List< string>();
foreach (ListItem l in CheckBoxList1.Items)
{
if (l.Selected == true )
i.Add(l.Value);
}
for ( int j = 0 ; j < i.Count; j ++)
{
if (s.Length > 0 )
s.Append(< span class =code-string> ,);
s.Append(i [j] .ToString());
// Label1.Text = s.ToString();
}
Response.Redirect( Default2.aspx?s = + s.ToString()) ;
页面上的
Default2.aspx以下代码检索用于的参数
string s;
s = Request.QueryString [ s];
SqlDataSource2.SelectParameters [ idbrand]。DefaultValue = s.ToString();
和搜索SQL代码
SelectCommand = SELECT id,idbrand,fname,camera,os,cameratype,ostype,price FROM tproduct WHERE(idbrand IN(@idbrand))> ;
但是,参数发送Default2.aspx?s = 1, 2 错误
转换失败 转换 nvarchar 值' 1,2' 数据类型 int 跨度>。
谢谢
解决方案
您不能将值列表作为参数字符串传递给SQL - 它不起作用,因为IN子句是在参数替换之前的阶段处理的。
这里有一个描述和解决方案:在SQL IN子句中使用逗号分隔值参数字符串 [ ^ ]
Hi
Sorry i do not speak much English
i have two pages names finder.aspx and defualt2.aspx
i am using the following code, i send send parameters to default2.aspx for search in database
StringBuilder s = new StringBuilder();
List<string> i = new List<string>();
foreach (ListItem l in CheckBoxList1.Items)
{
if (l.Selected == true)
i.Add(l.Value);
}
for (int j = 0; j < i.Count; j++)
{
if (s.Length > 0)
s.Append(",");
s.Append(i[j].ToString());
//Label1.Text = s.ToString();
}
Response.Redirect("Default2.aspx?s="+s.ToString());
on the page Default2.aspx the following code to retrieve the parameters used to
string s;
s = Request.QueryString["s"];
SqlDataSource2.SelectParameters["idbrand"].DefaultValue = s.ToString();
and SQL Code for Search
SelectCommand="SELECT id, idbrand, fname, camera, os, cameratype, ostype, price FROM tproduct WHERE (idbrand IN (@idbrand))">
but,parameters send Default2.aspx?s=1,2 error
Conversion failed when converting the nvarchar value '1,2' to data type int.
thank
解决方案
You can't pass a list of values into SQL as a parameter string - it doesn't work like that because the IN clause is processed at a stage before parameter substitution.
There is a description and solution here: Using comma separated value parameter strings in SQL IN clauses[^]
这篇关于将参数发送到另一个页面时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文