在url中运行webservice后出错 [英] Error after run webservice in url

查看:90
本文介绍了在url中运行webservice后出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



i写一个网络服务。

当我在localhost运行它工作正常。

但在上传到我的主机后,它也可以工作(在db中注册数据)。但是显示错误。



这里是错误信息:

'/'应用程序中的服务器错误。

对象引用未设置为对象的实例。

描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。



异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。



源错误:



在执行当前Web请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息。



堆栈跟踪:





[NullReferenceException:对象引用未设置为对象的实例。]

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



版本信息:Microsoft .NET Framework版本:4.0.30319 ; ASP.NET版本:4.7.3163.0





------------------ --------------------------------------------
$ b这里的$ b是webservice的代码

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

[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();

        }





谢谢



< b>我尝试了什么:



i不知道该怎么办,帮助



thanks

What I have tried:

i don't know what should i do, help

推荐答案

Quote:

异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Something is null,这就是您的应用无法继续的原因。很难分辨代码中的哪个位置和什么是null,所以请自己和我们一个忙,然后设置一个调试器并逐行查看什么是null并修复它。你的代码会运行;没有其他问题。

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();

        }

考虑阅读本文,了解如何连接到SQL Server数据库并对其执行命令,如何将SQL数据库连接到C#程序,初学者的教程 [ ^ ] ,这将是一个初学者的文章,所以没有复杂的东西,但总体基础知识在其中得到很好的解释。



并且您的SQL查询暴露于SQL注入,请始终考虑参数化您的查询,因为它们可以保护您的数据库免受SQL注入和不必要的数据丢失。



SQL注入 - 维基百科 [ ^ ]

.net - 什么对象引用未设置为对象的实例 意思? - 堆栈溢出 [ ^ ](在这个帖子中,有一个重复的主线程和主线程,也请读一个!)

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!)


这篇关于在url中运行webservice后出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆