错误:过程或函数sp_mandal指定了太多参数。 [英] Error:Procedure or function sp_mandal has too many arguments specified.

查看:58
本文介绍了错误:过程或函数sp_mandal指定了太多参数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在表格中插入记录..

如果名称已经存在于特定列中,我必须显示。用户名alredy已存在。

否则插入记录......



我试过这样......



Im trying to insert records in table..
if the name is already existed in particular column, i have to display. Username alredy existed.
else insert that records...

I have tried like this...

protected void Update_Click(object sender, EventArgs e)
    {
        int d = obj.check_mandal(txt_mname.Text);
        if (d > 0)
        {
            ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Mandal name already existed. kindly update it.');", true);
        }
        else
        {

            obj.mandals(txt_mname.Text, txt_desp.Text);

            ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Mandal details inserted Sucessfully');", true);

            txt_mname.Text = txt_desp.Text = "";
        }
    }










public int check_mandal(string s)
     {

         SqlConnection con = new SqlConnection(s1);
         cmd.Connection = con;
         cmd.CommandType = CommandType.StoredProcedure;
         cmd.CommandText = "sp_check_mandal";
         cmd.Parameters.AddWithValue("@mname", s);
         con.Open();
         int c = cmd.ExecuteNonQuery();
         con.Close();
         return c;
         }







public void mandals(string mname, string desp1)
    {

        SqlConnection con = new SqlConnection(s1);
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "sp_mandal";
        con.Open();

        cmd.Parameters.AddWithValue("@spmname", mname);

        cmd.Parameters.AddWithValue("@spdesp", desp1);


        cmd.ExecuteNonQuery();

        con.Close();
    }





sql ....





sql....

create procedure [dbo].[sp_mandal]
@spmname varchar(100),@spdesp varchar(max)
as
begin
insert into mandal(mname,desp) values(@spmname,@spdesp)
end










CREATE procedure [dbo].[sp_check_mandal](@mname varchar(100))
as
begin
select mname from mandal where mname=@mname
end







但它显示错误:




But it showing error:

Procedure or function sp_mandal has too many arguments specified.





请帮我解决这个问题..



提前致谢



Kindly help me to fix this issue..

Thanks in advance

推荐答案

这里的问题是,当您单击按钮时,您将在每个方法中一次又一次地向同一连接添加参数。

在每个方法中添加参数之前调用
Issue here is you are adding parameters to same connection in each method again and again when you click on button.
call
cmd.Parameters.Clear();



你最好创建像每个方法为SqlConnection做的命令对象。不要使用共享命令。

before adding parameters in each method.
you better create command object like you doing for SqlConnection for each method. Don't use shared command.


试试这个 -



static void GetSalesByCategory(string connectionString,string categoryName)

{

using(SqlConnection connection = new SqlConnection(connectionString))

{

//创建命令并设置其命令属性。

SqlCommand command = new SqlCommand();

command.Connection = connection;

command.CommandText =SalesByCategory;

command.CommandType = CommandType.StoredProcedure;



//添加输入参数并设置其属性。

SqlParameter parameter = new SqlParameter();

parameter.ParameterName =@ CategoryName;

parameter.SqlDbType = SqlDbType.NVarChar;

参数。 Direction = ParameterDirection.Input;

parameter.Value = categoryName;



//将参数添加到Parameters集合中。

command.Parameters.Add(参数);



//打开连接并执行阅读器。

connection.Open();

SqlDataReader reader = command.ExecuteReader();



if(reader.HasRows)

{

while(reader.Read())

{

Console.WriteLine({0}:{1: C,读者[0],读者[1]);

}

}

其他

{

Console.WriteLine(找不到行。);

}

reader.Close();

}

}
Try This-

static void GetSalesByCategory(string connectionString, string categoryName)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
// Create the command and set its properties.
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "SalesByCategory";
command.CommandType = CommandType.StoredProcedure;

// Add the input parameter and set its properties.
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "@CategoryName";
parameter.SqlDbType = SqlDbType.NVarChar;
parameter.Direction = ParameterDirection.Input;
parameter.Value = categoryName;

// Add the parameter to the Parameters collection.
command.Parameters.Add(parameter);

// Open the connection and execute the reader.
connection.Open();
SqlDataReader reader = command.ExecuteReader();

if (reader.HasRows)
{
while (reader.Read())
{
Console.WriteLine("{0}: {1:C}", reader[0], reader[1]);
}
}
else
{
Console.WriteLine("No rows found.");
}
reader.Close();
}
}


这篇关于错误:过程或函数sp_mandal指定了太多参数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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