Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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...

C#
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 = "";
        }
    }



C#
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



SQL
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
Posted

Issue here is you are adding parameters to same connection in each method again and again when you click on button.
call
C#
cmd.Parameters.Clear();
before adding parameters in each method.
you better create command object like you doing for SqlConnection for each method. Don't use shared command.
 
Share this answer
 
v2
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();
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900