0

Getting error -

Procedure or function 'sp_InsertContentForhomepage' expects parameter '@company', which was not supplied.

I'm trying to insert two values ...I'm new in using sp .

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ShoppingConnectionString"].ConnectionString);
con.Open();
string text = FCKeditor.Value;
string company = txtCompany.Text.Trim();
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand("sp_InsertContentForhomepage", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(@text, text);
cmd.Parameters.AddWithValue(@company,company);
cmd.ExecuteNonQuery();
con.Close();

My stored procedure:-

 create PROCEDURE sp_InsertContentForhomepage
   @company    VARCHAR(50),
    @text  VARCHAR(max)

AS
BEGIN

INSERT INTO tbl_HomepageContent (company, text)
VALUES (@company,@text)

END
  • Can you run SQL Profiler and check what values your code is passing to SP? – Rahul Singh Aug 14 '15 at 07:01
  • 3
    Missing quotes? `cmd.Parameters.AddWithValue("@company",company);` – Brendan Green Aug 14 '15 at 07:02
  • @BrendanGreen indeed... Would be obvious if OP use non-string types for variables... May be closed as duplicate of http://stackoverflow.com/questions/254669/what-does-placing-a-in-front-of-a-c-sharp-variable-name-do :) (also more like "typographical error"). – Alexei Levenkov Aug 14 '15 at 07:04
  • @BrendanGreen - His code won't even compile in that case, So he can't get that exception message at all. – Rahul Singh Aug 14 '15 at 07:04
  • @RahulSingh why??? `AddWithValue` takes 2 strings... so you can pass the same string twice no problem... Or you mean something else? – Alexei Levenkov Aug 14 '15 at 07:05
  • AddWithValue takes 2 arguments First with "@parametername" (must be in brackets) and then value... – Pedram Aug 14 '15 at 07:05
  • @AlexeiLevenkov - Yeah I meant `cmd.Parameters.AddWithValue(@company,company);` @company with quotes won't compile as it is not a string (I was pointing to OP's code and not to @Bredan's code :) ) – Rahul Singh Aug 14 '15 at 07:09
  • 1
    @RahulSingh I'm not really sure what do you mean `@company` is not a string (since it is the same as `company` without `@`): `AddWithValue(@company,company);` and `AddWithValue("@company",company);` are perfectly valid C# statements (since `string company;`). I'm not sure which version you believe will not compile. – Alexei Levenkov Aug 14 '15 at 07:13
  • @AlexeiLevenkov - Pardon me if I am wrong but how come `AddWithValue(@company,company)` (@company without any quotes which doesn't make it string) will compile with `AddWithValue(string,object)`? And in above comment it was a typo, `@company **without** quotes` – Rahul Singh Aug 14 '15 at 07:20
  • @RahulSingh `@company` is indeed string - maybe you've missed link in my first comment - http://stackoverflow.com/questions/254669/what-does-placing-a-in-front-of-a-c-sharp-variable-name-do which explains that `@company` is identical to `company` ("@" makes difference only for things like `@if`) – Alexei Levenkov Aug 14 '15 at 07:31
  • @AlexeiLevenkov - I agree that `company` is identical to `@company` but `company` is not a string at first place:- `string test = company` compilation error. `string test = "company";'` is valid similarly, `string test2 = @company` is invalid right..? – Rahul Singh Aug 14 '15 at 07:36
  • 1
    @RahulSingh but he has a variable named `company` in his code sample. If the variable `company` contained the value "test", then this `AddWithValue(@company, company)` is effectively the same as `AddWithValue("test", company)`. See https://msdn.microsoft.com/en-us/library/aa664670.aspx – Brendan Green Aug 14 '15 at 12:38

2 Answers2

1

I think you should write like this,

cmd.Parameters.Add("@text", text);
cmd.Parameters.Add("@company", company);

Try by doing above....

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Pedram
  • 6,256
  • 10
  • 65
  • 87
  • 2
    What version of `Add` expects two parameters with ParameterName & Value? – Rahul Singh Aug 14 '15 at 07:16
  • @RahulSingh - https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx to know more about it. Also have a look at this you will get more idea. http://stackoverflow.com/questions/14640094 – Pedram Aug 14 '15 at 09:57
  • @krishnamohan - your simple mistake was, you missed to do quotes like "@text"... it can also solve your problem. – Pedram Aug 14 '15 at 09:58
  • You gave the reference links but have you checked them? None out of 4 overloads of `SqlCommand.Parameters.Add` method accepts `parameterName`& `Value`. – Rahul Singh Aug 14 '15 at 10:02
  • Questioner has already marked as answer as it was working solution for him. Then who has downvoted without comments? Not good! Please give some comment. If you don't have any reason do not do down vote. – Pedram Oct 31 '15 at 05:21
0

Have you checked value in string company = txtCompany.Text.Trim(); Be sure you are not sending a null value. IF company is not null the do the following:

create procedure sp_InsertContentForhomepage
    @company varchar(50),   
    @text varchar(max)
as
begin
    insert into tbl_HomepageContent
    values (@company,@text)
end

And I think you must send value like cmd.Parameters.AddWithValue("@company", company);

TFrost
  • 769
  • 2
  • 12
  • 31