Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

asp.net c# 2.0 stored procedure error

Posted on 2006-05-01
4
Medium Priority
?
341 Views
Last Modified: 2008-02-07
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'
0
Comment
Question by:fwsteal
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
here4u247 earned 2000 total points
ID: 16579279
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
 

Author Comment

by:fwsteal
ID: 16579424
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
 

Author Comment

by:fwsteal
ID: 16579541
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
 
LVL 23

Expert Comment

by:naveenkohli
ID: 16579867
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question