日期:2014-05-18  浏览次数:20444 次

asp.net2.0数据源参数设置问题(十万火急)
如果在数据源中设置参数并传递数值?
以下代码报错索引超出范围。必须为非负值并小于集合大小,应该系未添加参数
C# code
 SqlDataSource1.ConnectionString=...;
            SqlDataSource1.SelectCommand = "SELECT url FROM Ad where url=@url";
            SqlDataSource1.SelectParameters[0].DefaultValue = "123";
            DataList1.DataBind();


------解决方案--------------------
string conStr = ConfigurationManager.ConnectionStrings["northwindConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand("GetAllEmployeesID", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("@id", SqlDbType.Int);
p1.Value = EID;
cmd.Parameters.Add(p1);
con.Open();
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return reader;
------解决方案--------------------
SqlDataSource1.SelectParameters[0].DefaultValue = "123";
SqlDataSource1 是否声明了@url
------解决方案--------------------
<asp:sqldatasource
id="SqlDataSource1"
runat="server"
connectionstring="<%$ ConnectionStrings:MyNorthwind%>"
selectcommand="SELECT LastName FROM Employees WHERE Title = @Title">
<selectparameters>
<asp:controlparameter name="Title" controlid="DropDownList1" propertyname="SelectedValue"/>
</selectparameters> </asp:sqldatasource>

注意代码的红色部分,必须声明SelectParameters才可以
------解决方案--------------------
应该是没有声明参数造成的
建议先声明参数试试

C# code

SqlDataSource1.SelectParameters.Add("Url", TypeCode.String, 64);

------解决方案--------------------
试试这样写:
C# code

SqlDataSource1.SelectParameters.Add(new Parameter("url", TypeCode.String, "123"));