【问题标题】:Error when Using queryString使用 queryString 时出错
【发布时间】:2015-02-10 03:53:30
【问题描述】:

当我尝试获取 URL 并将其传递给过程时 它没有通过

string ID = Request.QueryString["URL"];    
youtube y = new youtube();
y.URL = ID;
repCurrentVideo.DataSource = y.CurrentVideo();
repCurrentVideo.DataBind();
lblDetails.Text = "no details available about this video";

显示给我的错误说 过程或函数“currentvideo”需要参数“@URL”,但未提供该参数。 异常详细信息:System.Data.SqlClient.SqlException:过程或函数“currentvideo”需要参数“@URL”,但未提供。

这是存储过程调用代码

public DataTable CurrentVideo() 
{
    CreateConnection();
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@URL", URL);
    SqlDataAdapter da = new SqlDataAdapter("currentvideo", conn);
    DataTable dt = new DataTable();
    da.Fill(dt);
    return dt;
}

【问题讨论】:

  • 您的请求是什么样的?

标签: c# asp.net stored-procedures


【解决方案1】:

您永远不会将 SqlCommand (cmd) 添加到您的数据适配器中

试试这样的:

cmd.Parameters.AddWithValue("@URL", URL);
da.SelectCommand = cmd;

CreateConnection();
SqlCommand cmd = new SqlCommand("currentvideo", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@URL", URL);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;

此外,虽然我们讨论的是 SqlCommands,但我认为最好将它们包装在 using 语句中,因为它们的类型为 IDisposable

using (SqlCommand command = new SqlCommand("currentvideo", conn)) {
    ...
}

查看此链接以查看其外观:

http://www.dotnetperls.com/sqlcommand

编辑: 因此,根据您的 URL,您正在寻找错误的查询字符串参数。你需要把ID的赋值改成这样:

string ID = Request.QueryString["ID"];

【讨论】:

  • 给我同样的错误过程或函数'currentvideo'需要参数'@URL',但未提供。
  • @MahmoudZakal 您能否发布您更新的代码并进行调试以查看URL(或Request.QueryString["URL"];)的值是多少?
  • 我调试了,ID的值还是空
  • 好的@MahmoudZakal,您请求的网址是什么样的?
  • 我的项目看起来像我在主页视频名称中的 youtube,然后当我点击名称时,在 ShowVideo 页面中打开视频
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-08
  • 1970-01-01
  • 1970-01-01
  • 2011-12-27
  • 1970-01-01
  • 2013-10-20
  • 1970-01-01
相关资源
最近更新 更多