Click here to Skip to main content
15,881,882 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.

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.
 
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