Click here to Skip to main content
14,920,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have these codes but they don't seem to insert the values

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

SQL
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
Updated 15-Feb-13 2:33am
v3
Comments
Chris Reynolds (UK) 15-Feb-13 8:15am
   
Can we see the contents of your 'InsClass' stored procedure?
   
Debug your code, which calls this InsertClass function and see line by line execution.
Chris Reynolds (UK) 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 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) 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.
fjdiewornncalwe 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) 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 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 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) 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 15-Feb-13 10:07am
   
Thank you very much, I tried it and it worked fine. It is greatly appreciated.

1 solution

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.
   

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