Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I have these codes but they don't seem to insert the values
 
[DataObjectMethod(DataObjectMethodType.Insert)]
    public static void InsertClass(string name, string code)
    {
        String strConnString = ConfigurationManager.ConnectionStrings["BookstoreAppConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "InsClass";
 
        cmd.Parameters.AddWithValue("@name", name);
        cmd.Parameters.AddWithValue("@code", code);
        //cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = txtname.Text.Trim();
        //cmd.Parameters.Add("@code", SqlDbType.VarChar).Value = txtcode.Text.Trim();
        cmd.Connection = con;
        
            con.Open();
            cmd.ExecuteNonQuery();
            //lblMessage.Text = "Record inserted successfully";
     
        
            con.Close();
 
    }
 
[update from comments below:]
 
the stored procedure is:

ALTER procedure [dbo].[InsClass]
(
@name varchar(50),
@code varchar(50)
)
as 
begin
set nocount on;
insert into Class(ClassName, ClassCode)
values(@name, @code)
end
Posted 15-Feb-13 2:09am
Edited 15-Feb-13 2:33am
v3
Comments
Chris Reynolds (UK) at 15-Feb-13 8:15am
   
Can we see the contents of your 'InsClass' stored procedure?
Tadit Dash at 15-Feb-13 8:35am
   
Debug your code, which calls this InsertClass function and see line by line execution.
Chris Reynolds (UK) at 15-Feb-13 8:50am
   
Just to confirm. Are you seeing any error message/exceptions raised or does your code run through cleanly but no rows are appearing on the database.
 
Have you tried testing your stored procedure using SQL Server Management Studio (or Query Analyser)?
 
When you have a problem like this it does help to break it into pieces.
Member 8497287 at 15-Feb-13 9:08am
   
I tested the stored procedure and it works fine in sql management studio but when I use the business class I get an error saying that the expected parameters are not bwing passed, when I debug the code it shows that the values are being passed as null
Chris Reynolds (UK) at 15-Feb-13 9:14am
   
Thank you, that helps. Are you saying that your variables in the C# code called 'name' and 'code' are null. What happens if you hard code a value like this:
cmd.Parameters.AddWithValue("@name", "Test Name");
cmd.Parameters.AddWithValue("@code", "Test Code");
does the code work then? If it does then you need to look into whatever is calling this method as it may be passing in null values.
Marcus Kramer at 15-Feb-13 9:31am
   
Chris, you should post your comments as a solution. You may as well get credit for the solution so one of the rep-whores doesn't grab them.
Chris Reynolds (UK) at 15-Feb-13 9:53am
   
You're probably right - I'm only doing this to build my skills up while I look for a job so I can't be too bothered about the rep thing. Thanks for the thought.
Member 8497287 at 15-Feb-13 9:35am
   
It doesn't work, i get an error:
 
Procedure or function 'InsClass' expects parameter '@name', which was not supplied.
Fred Flams at 15-Feb-13 9:53am
   
I believe that Chris is right and that whatever calls the Insert method is passign null values instead of valid strings. Actually, you should test for null strings at the start of your method InsertClass and throw an exception at that point rather than trying to insert wrong data in your database.
Chris Reynolds (UK) at 15-Feb-13 9:52am
   
I have just taken your C# code and your stored proc and created a similar set up here and it is working fine. If I pass null in as my name and code then I get the error you describe.
So a call to your function like this works fine:
InsertClass("King", "Lear");
and this one gives the error:
InsertClass(null, null);
Can you step through your InsertClass method and verify whether name and class are null. I am sure that if you pass them in hard coded then it should work fine as there is nothing wrong with the way you are calling the SP that I can see.
Member 8497287 at 15-Feb-13 10:07am
   
Thank you very much, I tried it and it worked fine. It is greatly appreciated.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Posted as a solution so we can close this down:
 
It looks like the OP was passing null values into his function and needs to check for this as Fred says before attempting to insert into SQL.
  Permalink  

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

  Print Answers RSS
0 Nirav Prabtani 268
1 OriginalGriff 217
2 Mika Wendelius 185
3 CPallini 170
4 _Amy 170
0 Nirav Prabtani 284
1 OriginalGriff 217
2 Mika Wendelius 185
3 _Amy 170
4 Sergey Alexandrovich Kryukov 154


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100