Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hello

i write a webservice.
when i run it in localhost it work fine.
but after upload it in my host, it also work (register data in db). but show a error.

here is error message:
Server Error in '/' Application.
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.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
DOAdverisingModule.AdsModule.context_PostRequestHandlerExecute(Object sender, EventArgs e) +295
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +141
System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +71

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3163.0


--------------------------------------------------------------
here is code of webservice
C#
[WebMethod]
        public void insert_into_test_tbl(string name, int grade)
        {
            con = new SqlConnection(conString);
            SqlCommand cmd = con.CreateCommand();
            SqlTransaction tr;


            string comm = "insert into test_tbl (name, grade) VALUES(N'" +
                name + "', " +
                grade + ") ";

            cmd.CommandText = comm;

            if (con.State == ConnectionState.Closed)
                con.Open();

            tr = con.BeginTransaction();
            cmd.Transaction = tr;
            cmd.ExecuteNonQuery();
            tr.Commit();

        }


thanks

What I have tried:

i don't know what should i do, help
Posted
Updated 10-Dec-18 7:07am
v2

1 solution

Quote:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Something is null, and that is why your app cannot proceed. It is really hard to tell where and what is null in the code, so do yourself and us a favor and set a debugger and go line-by-line to see what is null and fix it. Your code will run; provided no other issues are there.
[WebMethod]
        public void insert_into_test_tbl(string name, int grade)
        {
            con = new SqlConnection(conString); // conString, where is it? 
            SqlCommand cmd = con.CreateCommand(); // Why not new SqlCommand("...");?
            SqlTransaction tr;


            string comm = "insert into test_tbl (name, grade) VALUES(N'" +
                name + "', " +
                grade + ") ";

            cmd.CommandText = comm;

            if (con.State == ConnectionState.Closed) // Supposedly true always
                con.Open();

            tr = con.BeginTransaction();
            cmd.Transaction = tr;
            cmd.ExecuteNonQuery(); // Your command is not attached to a SQL Server
            tr.Commit();

        }
Consider reading this article of mine to get to know how to connect to your SQL Server databases and execute commands on them, How to connect SQL Database to your C# program, beginner's tutorial[^] , this will be a beginner's article so no complex things, but overall basics are well explained in it.

And your SQL queries are exposed to SQL Injection, please always consider parametrizing your queries, as they can protect your databases from SQL Injections and unwanted data loss.

SQL injection - Wikipedia[^]
.net - What does "Object reference not set to an instance of an object" mean? - Stack Overflow[^] (On this thread, there is a previous thread that is duplicate and primary, do read that one too!)
 
Share this answer
 
v3

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