Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey Friends,

I'm trying to store values in database in stored procedure, but neither they are going in the database nor they are getting stored.

This is my stored procedure:-

SQL
ALTER proc [dbo].[cards]
(
	@comname		varchar(100),
	@commes		varchar(100),
	@job			varchar(50),
	@name		varchar(200),
	@add1		varchar(200),
	@add2		varchar(200),
	@add3		varchar(200),
	@phone		varchar(10),
	@fax			varchar(100),
	@email		varchar(100)
)
as
begin
	insert into card1 (comname,commes,job,name,add1,add2,add3,phone,fax,email)
	values (@comname,@commes,@job,@name,@add1,@add2,@add3,@phone,@fax,@email)	
	end


And this is my C#:-

C#
protected void Button1_Click(object sender, EventArgs e)
    {
        string str = @"data source=DEEPAKSHARMA-PC\SQLEXPRESS; initial catalog=new; integrated security=true";
        SqlConnection con = new SqlConnection(str);
        con.Open();
        try
        {
            if (IsPostBack != true)
            {
                SqlCommand cmd = new SqlCommand("cards", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Text11.Value.Trim());
                cmd.Parameters.Add(Text12.Value.Trim());
                cmd.Parameters.Add(Text13.Value.Trim());
                cmd.Parameters.Add(Text14.Value.Trim());
                cmd.Parameters.Add(Text15.Value.Trim());
                cmd.Parameters.Add(Text16.Value.Trim());
                cmd.Parameters.Add(Text17.Value.Trim());
                cmd.Parameters.Add(Text18.Value.Trim());
                cmd.Parameters.Add(Text19.Value.Trim());
                cmd.Parameters.Add(Text20.Value.Trim());
                cmd.ExecuteNonQuery();
            }
        }

        catch (Exception ex)
        {
            Console.WriteLine("Something went wrong");
        }

        finally
        {
            con.Close();
            con.Dispose();
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Data Inserted Successfully')", true);
        }

    }


Another problem is whenever I refresh the page it shows the message "Data Inserted Successfully". PLEASE HELP!!
Posted
Updated 20-Apr-15 17:22pm
v2
Comments
Deepak Kanswal Sharma 20-Apr-15 21:59pm    
At first it wasn't showing me any table in my database, then I created the table card1 in "new" database.

I think I'm doing something very wrong! :(

Hi Deepak,

Please debug your stored procedure in management studio like visual studio code debugging.

DECLARE @comname AS varchar(100)
DECLARE @commes AS varchar(100)
DECLARE @job AS varchar(50)
DECLARE @name AS varchar(200)
DECLARE @add1 AS varchar(200)
DECLARE @add2 AS varchar(200)
DECLARE @add3 AS varchar(200)
DECLARE @phone AS varchar(10)
DECLARE @fax AS varchar(100)
DECLARE @email AS varchar(100)


SET @comname =''
SET @commes =''
SET @job=''
SET @name =''
SET @add1=''
SET @add2=''
SET @add3 =''
SET @phone =''
SET @fax =''
SET @email =''

-- ALTER proc [dbo].[cards]
--(
-- @comname varchar(100),
-- @commes varchar(100),
-- @job varchar(50),
-- @name varchar(200),
-- @add1 varchar(200),
-- @add2 varchar(200),
-- @add3 varchar(200),
-- @phone varchar(10),
-- @fax varchar(100),
-- @email varchar(100)
--)
--as
begin
insert into card1 (comname,commes,job,name,add1,add2,add3,phone,fax,email)
values (@comname,@commes,@job,@name,@add1,@add2,@add3,@phone,@fax,@email)
end
 
Share this answer
 
Comments
Deepak Kanswal Sharma 21-Apr-15 2:43am    
Thanks for the help!!
But I can't understand the difference. Why declare my values when I've already made the table in database. And why is that the table is not available in the database.
Deepak Kanswal Sharma 21-Apr-15 8:33am    
While trying that code my server gives the error:- "Unable to save object because the statement is not supported. It must begin with CREATE or ALTER".
Well I got the solution:-

protected void Button1_Click(object sender, EventArgs e)
{
string str = @"data source=DEEPAKSHARMA-PC\SQLEXPRESS; initial catalog=new; integrated security=true";
SqlConnection con = new SqlConnection(str);
try
{
//if(Page.IsPostBack!=true)
{
SqlCommand cmd = new SqlCommand("cards", con);
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@comname",Text11.Value.Trim());
cmd.Parameters.Add("@commes",Text12.Value.Trim());
cmd.Parameters.Add("@job",Text13.Value.Trim());
cmd.Parameters.Add("@name",Text14.Value.Trim());
cmd.Parameters.Add("@add1",Text15.Value.Trim());
cmd.Parameters.Add("@add2",Text16.Value.Trim());
cmd.Parameters.Add("@add3",Text17.Value.Trim());
cmd.Parameters.Add("@phone",Text18.Value.Trim());
cmd.Parameters.Add("@fax",Text19.Value.Trim());
cmd.Parameters.Add("@email",Text20.Value.Trim());
cmd.ExecuteNonQuery();
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Data Inserted Successfully')", true);
}
}

catch (Exception ex)
{
Console.WriteLine("Something went wrong");
}

finally
{
con.Close();
con.Dispose();

}

}


The difference is I was not using the column name in the cmd.parameter field.
 
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