【问题标题】:passing a datalist value using session in asp.net在 asp.net 中使用会话传递数据列表值
【发布时间】:2015-05-29 11:20:15
【问题描述】:

我实际上是使用会话存储一个值,然后使用图像按钮重定向到其他页面,其中基于会话值我从数据库中获取数据。我在该行中收到用户定义的异常。 请帮忙!

adap.Fill(dt1);//[SqlException (0x80131904): Incorrect syntax near '='.]

这是我的编码。

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
    ImageButton btn = sender as ImageButton;
    string modelId = btn.CommandArgument;
    Session["modelid"] = modelId;
    Response.Redirect("details.aspx");
} 

public partial class details : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=VISH;Initial Catalog=VISH;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {
        con.Open();
        SqlDataAdapter adap = new SqlDataAdapter("select * from details1 where Modelid=" + Session["modelid"], con);
        DataTable dt1 = new DataTable();
        adap.Fill(dt1);
        DataList1.DataSource = dt1;
        DataBind();
    }

【问题讨论】:

  • Session["modelid"] 有什么值?
  • 因为我在两个 d 页面中都使用 datalist,所以我从数据库中获取它。这是datalist中imagebutton的设计部分

标签: c# asp.net session datalist


【解决方案1】:

首先将会话对象转换为字符串。

(String) Session["modelid"] 

【讨论】:

  • 我应该在哪里做呢?在我声明会话的页面中还是在我使用会话从数据库中选择数据的页面中?
【解决方案2】:

我建议您使用 avoid using string concatenation 创建命令 - 改用 parameters

另外,你必须检查Session["modelid"] 是否有值,因为空值的参数会导致给定的消息:

参数化查询'(@id nvarchar(4000))select * from details1 其中 Modelid=@id' 需要参数“@id”,但未提供该参数

所以,代码将是:

protected void Page_Load(object sender, EventArgs e)
{
    con.Open();

    var id = Session["modelid"];

    if (id != null)
    {        
        SqlDataAdapter adap = new SqlDataAdapter("select * from details1 where Modelid=@id" , con);
        adap.SelectCommand.Parameters.AddWithValue("@id", );

        DataTable dt1 = new DataTable();
        adap.Fill(dt1);
        DataList1.DataSource = dt1;
        DataBind();
    }
}

如果保证Session["modelid"] 将在第一次回发后设置,则使用Page.IsPostBack 属性。

或者只是使用一些默认值。

P.S.:另外,not a good idea 使用实例级连接,您不能以确定的方式Dispose (Close) 它。最好将其设为方法级变量,并与using 子句一起使用:

protected void Page_Load(object sender, EventArgs e)
{
     using (SqlConnection con = new SqlConnection("Data Source=VISH;Initial Catalog=VISH;Integrated Security=True"))
     {
         // ...        
     }
}

【讨论】:

  • 现在这是异常参数化查询'(@id nvarchar(4000))select * from details1 where Modelid=@id' 需要参数'@id',但未提供。跨度>
  • @sharanya 嗯,其实是提供的——AddWithValue("@id", Session["modelid"]);。奇怪,我会检查一下 - 也许SqlDataAdapter 的参数化查询有一些问题。
  • 因为我在两个 d 页面中都使用 datalist,所以我从数据库中获取它。这是datalist中imagebutton的设计部分 和我上面提到的编码部分你能告诉我这可能是什么错误? ——
  • @sharanya 好吧,我试过了,SqlDataAdapter 对有参数的命令没有问题。看来问题出在Session["modelid"] 上。它的值是多少,Modelid 列的类型是什么?
  • 正如我在上一条评论中提到的,我正在从数据库中获取模型 ID,并且我正在使用图像按钮重定向到其他页面,该页面将显示有关单击的图像的更多详细信息。为此,我在两个页面中都使用了 DataList。在第一页它正在从数据库中获取值并显示它没有问题。但是,为了显示特定细节,我在 ImageButton 中使用了 CommandArgument='
猜你喜欢
  • 2012-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-15
  • 1970-01-01
  • 2012-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多