@Flag不是Procedure SPPartTable的参数 [英] @Flag not parameter for Procedure SPPartTable

查看:68
本文介绍了@Flag不是Procedure SPPartTable的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hii我是asp.net的新手

我在同一页面上调用了两个存储过程,两者都是在回发之前调用的。第一个过程调用用于将数据绑定到GridView,第二个用于下拉列表。我为这两个过程传递@Flag。当程序执行一个绑定方法为Gridview正确执行但在bindDll它给出错误@Flag发送多次。

任何人都可以帮助我

谢谢。

*******************

这是我的代码

******** ***********************************

Hii I'm New in asp.net
I call two stored Procedure at same page ,both are call before post back. First procedure call is use to bind data to GridView and second one is used for dropdown list .I pass a @Flag for both procedure. When program execute a bind Method for Gridview execute properly but at bindDll it gives error @Flag send Multiple time.
Can anybody help me
Thank you.
*******************
This is my code
***************************************

String strConnString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
SqlConnection con;
SqlCommand cmd = new SqlCommand();
string @value;
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGridView();
        Binddl();
    }

}

private void BindGridView()
{
    
    con = new SqlConnection(strConnString);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@Flag", 11);
    cmd.CommandText = "StoreProcedureSelect";

    cmd.Connection = con;
    try
    {
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        //GridView1.DataSource = ds;
        //GridView1.DataBind();
        if (ds.Tables[0].Rows.Count == 0)
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            grdvwParts.DataSource = ds;
            grdvwParts.DataBind();
            int columncount = grdvwParts.Rows[0].Cells.Count;
            grdvwParts.Rows[0].Cells.Clear();
            grdvwParts.Rows[0].Cells.Add(new TableCell());
            grdvwParts.Rows[0].Cells[0].ColumnSpan = columncount;
            grdvwParts.Rows[0].Cells[0].Text = "No Records Found";
        }
        else
        {
            grdvwParts.DataSource = ds;
            grdvwParts.DataBind();
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        con.Close();
        con.Dispose();
    }
}
public void Binddl()
{

    try
    {
        con = new SqlConnection(strConnString);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Flag",1);
        cmd.CommandText = "SPPartTable";
        
        cmd.Connection = con;
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd);

        DataTable dt = new DataTable();

        da.Fill(dt);
        ddlsection.DataSource = dt;
        ddlsection.DataBind();
        ddlsection.DataTextField = "sdesc";
        ddlsection.DataValueField = "sec_id";
        ddlsection.DataBind();

    }
    catch (Exception e)
    {

        throw e;
    }
    finally
    {
        con.Close();
        con.Dispose();

    }


}

推荐答案

<$ c在第二种方法中使用的$ c> command 实际上是持有前一个参数。您需要在第二个方法内重新初始化,就像您为连接所做的那样。如下所示...

The command used in the second method is actually holding the previous parameter. You need to reinitialize inside the second method, as you have done for the connection. So do like below...
con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand("SPPartTable", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Flag",1);



注意:当您使用适配器,因为它们会自动为您打开和关闭。


NOTE: No need to open and close connections when you are using the Adapters, because they automatically open and close for you.


这篇关于@Flag不是Procedure SPPartTable的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆