【问题标题】:Using SqlDataSource In Visual Studio在 Visual Studio 中使用 SqlDataSource
【发布时间】:2015-08-08 15:45:04
【问题描述】:

我在 Visual Studio 中遇到了一个有趣的问题(可能是由于我缺乏经验)。我有一个 SqlDataSource 对象和一个 GridView 对象。 Gridview 对象设置为使用 SqlDataSource 作为源。

SqlDataSource 使用参数(Source = Control,它是一个文本框)作为输入传递给 SQL 存储过程以返回记录集。当我单步执行代码时,在更改到文本框时,我执行SqlDataSource.Insert(),然后执行GridView.DataBind()

由于我遇到了无法获得预期结果的问题,因此我在 SQL Server Profiler 中运行了一个恍惚状态。

好像SqlDataSource.Insert()运行时,通过参数传递了一个空值。当GridView.DataBind()在下一行运行时,参数值正确传递。

任何想法可能导致这种情况?我相信我在这里做错的任何事情也会影响我代码的其他领域。

protected void ItemBox_TextChanged(object sender, EventArgs e)
{
    if(setFire)
    {
        SqlDataSource1.Insert();
        GridView1.DataBind();
        Label1.Text = GridView1.Rows.Count.ToString() + " Results Found";
        Label2.Text = ItemBox.Text;

        setFire = false;
        ItemBox.Text = "";
        ItemBox.Focus();
        setFire = true;
    }
}

【问题讨论】:

  • 为什么我们在这里有 SqlDataSource.Insert,我没有看到任何数据库插入的东西。 ?
  • 老实说,我只是想执行存储过程。我想我需要使用 .Insert() 因为我正在尝试将参数值推送到存储过程。
  • 为此添加一点。我有第二个返回输出参数的 SqlDataSource。我也在尝试执行它,但是我遇到了与此处通过 NULL 相同的问题。

标签: c# asp.net visual-studio gridview sqldatasource


【解决方案1】:

我能够解决我的问题。我有两个需要执行的存储过程。一个与 GridView 相关联,另一个只是返回一个我将在屏幕上显示的输出参数。

使用 SqlDataSource1(绑定到我的 GridView 的对象)我不需要调用 SqlDataSource1.Insert()。只需执行 GridView1.DataBind() 命令即可。

至于其他我无法让 SqlDataSource 对象表现得像我想要的那样,所以我最终使用了下面 youtube 视频中概述的方法:

https://www.youtube.com/watch?v=stIWeAcO45Y

它最终完美运行。

非常感谢@alainlompo 在上面提供的帮助。虽然我最终没有使用该技术,但我确实发现您的示例非常有用。

【讨论】:

    【解决方案2】:

    在调用 SqlDataSource 上的 Insert 方法之前,您需要正确定义其 InsertCommand。以下是你可以做到的:

    protected void ItemBox_TextChanged(object sender, EventArgs e)
    {
        if(setFire)
        {
            SqlDataSource1.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;
            string storedProcedureName = .... // The name of your stored procedure
    
            SqlDataSource1.InsertCommand= storedProcedureName;
    
            // Then you should add a SqlParameter object into the collection
            // of Parameters for the Insert Command
            // Its name correspond to the variable name expected by your
            // stored procedure and its value comes from your textbox
            string paramName = .... // The parameter name in your stored procedure
    
            SqlDataSource1.InsertParameters.Add(paramName, yourTextBoxName.Text);
    
            // Now you can call Insert()
    
            SqlDataSource1.Insert();
            GridView1.DataBind();
            Label1.Text = GridView1.Rows.Count.ToString() + " Results Found";
            Label2.Text = ItemBox.Text;
    
            setFire = false;
            ItemBox.Text = "";
            ItemBox.Focus();
            setFire = true;
        }
    }
    

    【讨论】:

    • 我尝试了您的解决方案,但出现以下错误:“过程或函数指定的参数过多。”。你认为这是因为我已经通过配置数据源 GUI 进行了一些设置吗?
    • 另外,我将如何调整它以使用输出参数?我有另一个 SqlDataSource 正在使用具有两个输入和一个输出参数的第二个存储过程。感谢您迄今为止的帮助!
    • @user2061929,是的,可能与初始设置发生冲突。我将通过更详细的示例进行进一步调查。最美好的祝愿
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多