asp.net c# 2.0 stored procedure error

I've got the following insert to a stored procedure and am getting errors. I can't find a solution. Help.

1.  String strConnection;
2.  strConnection = ConfigurationManager.AppSettings("MyConnectionString"); //error
3.  string sConnStr = strConnection.ToString();
4.  SqlConnection cnBKTest = new SqlConnection(sConnStr);
5.  SqlCommand cmdInsert = new SqlCommand("usp_Insert", cnBKTest);
6.  cmdInsert.CommandType = Data.CommandType.StoredProcedure; //error
7.  cmdInsert.Parameters.Add(new SqlParameter("@Model", Data.SqlDbType.smallint, 2)); //error
8.  cmdInsert.Parameters.Add(new SqlParameter("@Name", Data.SqlDbType.varchar, 255));
9.  cmdInsert.Parameters.Add(new SqlParameter("@MonthTerms", Data.SqlDbType.tinyint, 1));
10. cmdInsert.Parameters("@Model").Value = MModel; //error
11. cmdInsert.Parameters("@Name").Value = MName;
12. cmdInsert.Parameters("@MonthTerms").Value = MMonth;

cnBKTest.Open();
 if (cnBKTest.State == ConnectionState.Open)
  {
   //cmdInsert.ExecuteNonQuery();
   LabelError.Text = "Open";
  }
 else
  {
   LabelError.Text = "Error";
  }
cnBKTest.Close();

-------------------------------

Line Errors:
2.  'System.Configuration.ConfigurationManager.AppSettings' is a 'property' but is used like a 'method'
6.  The name 'Data' does not exist in the current context
7.  The name 'Data' does not exist in the current context
10. 'System.Data.SqlClient.SqlCommand.Parameters' is a 'property' but is used like a 'method'
fwstealAsked:
Who is Participating?
 
here4u247Connect With a Mentor Commented:
Hello,


1- change from () to [] i.e.
ConfigurationManager.AppSettings["MyConnectionString"];
2-change to System.Data.CommandType.StoredProcedure;
3-same as above
4-same as no 1, change to () to []

hope that helps


0
 
fwstealAuthor Commented:
Great!

How would I write the following? Do I need to specify the size for smallint and tinyint?

7.  cmdInsert.Parameters.Add(new SqlParameter("@Model", Data.SqlDbType.smallint, 2)); //error
8.  cmdInsert.Parameters.Add(new SqlParameter("@Name", Data.SqlDbType.varchar, 255));
9.  cmdInsert.Parameters.Add(new SqlParameter("@MonthTerms", Data.SqlDbType.tinyint, 1));
0
 
fwstealAuthor Commented:
I did this and no syntax error:
 cmdInsert.Parameters.Add(new SqlParameter("@Model", System.Data.CommandType.StoredProcedure));
 cmdInsert.Parameters.Add(new SqlParameter("@Name", System.Data.CommandType.StoredProcedure));
 cmdInsert.Parameters.Add(new SqlParameter("@MonthTerms", System.Data.CommandType.StoredProcedure));


However, I got the error below


3.  string sConnStr = strConnection.ToString();

Error:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request.
Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
0
 
naveenkohliCommented:
Check your configuration file for "MyConnectionString" key value. Your call to get this value is returning null. Thats why you are getting the exception about null objecr reference.
You should be checking for null value before calling any method on any object that you get from confgiuration files.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.