Click here to Skip to main content
Sign Up to vote bad
good
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:09
Edited 15 Feb '13 - 2:33

Comments
Chris Reynolds (UK) - 15 Feb '13 - 8:15
Can we see the contents of your 'InsClass' stored procedure?
Tadit Dash - 15 Feb '13 - 8:35
Debug your code, which calls this InsertClass function and see line by line execution.
Chris Reynolds (UK) - 15 Feb '13 - 8:50
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:08
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:14
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 - 15 Feb '13 - 9:31
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:53
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:35
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:53
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:52
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:07
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.
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 8,028
1 OriginalGriff 6,010
2 CPallini 3,432
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid