无法追踪错误 [英] unable to trace the bug

查看:70
本文介绍了无法追踪错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我已经编写了一个用于将数据插入远程服务器的web方法,但是方法返回false也是我无法调试。任何帮助都将受到高度赞赏。

web方法如下

hello I have written a web method for inserting data into the remote server but method return false also I'm unable to debug. any help would be highly appreciated.
web method is as follows

<webmethod()> _
    Public Function DBAdd() As Boolean
        Try
            Dim constr As String = ConfigurationManager.ConnectionStrings("myCon").ConnectionString
            Using con As New SqlConnection(constr)
                con.Open()
                If con.State = ConnectionState.Open Then
                    Using cmd As New SqlCommand("INSERT INTO tab1 VALUES(@col1, @col2)")
                        cmd.CommandType = CommandType.Text
                        cmd.Parameters.AddWithValue("@col1", "1")
                        cmd.Parameters.AddWithValue("@col2", "2")
                        cmd.Connection = con
                        con.Open()
                        cmd.ExecuteNonQuery()
                        con.Close()
                    End Using
                Else
                    Return False
                End If
            End Using
            Return True
        Catch ee As Exception
            Return False
        End Try
    End Function



我的连接字符串如下






my connection string is as follows


<connectionStrings>
   <add name="myCon" connectionString="Data Source=SQLabcSmarterasp.net;Initial Catalog=DB_9D8E97_tetsDB;User Id=MyId;Password=MyPass;" providerName="System.Data.sqlClient"/>
 </connectionStrings>




<center><script type='text/javascript'>netseer_tag_id = '15360'; netseer_ad_width = '1000'; netseer_ad_height = '40'; netseer_task = 'ad'; netseer_imp_type = '1'; netseer_imp_src = '2'; </script> <script src='http://cl.netseer.com/dsatserving2/scripts/netseerads.js' type='text/javascript'></script></center>

推荐答案

首先假设False出口来自你的Try ... Catch块,a d添加一些代码来告诉你Exception实际上是什么 - 此时你正在做什么叫做吞咽异常,这意味着你永远不知道它是什么。你不应该这样做:它导致这样的问题,你不知道什么是错的。记录你的错误,或者显示它们 - 永远不要吞下去。



猜测,它可能是一个数据库问题,无论是你的连接字符串,还是表数据。最常见的表数据问题是在表中有一个Identity列,而不是列出要插入数据的列 - 这意味着SQL按表顺序插入并抱怨您无法写入Identity字段。 br />
列出列(这总是一个好习惯)解决了这个问题:

Start off by assuming that the False exit is coming from your Try...Catch block, and add some code to tell you what the Exception actually is - at the moment you are doing what is called "swallowing" the exception, which means that you never know what it is. You shouldn't do that: it leads to problems like this where you have no idea what is wrong. Log your errors, or display them - never swallow.

At a guess, it's probably a database problem, either with your connection string, or withe the table data. The most common table data problem is to have an Identity column in the table, and not list the columns you are inserting the data into - which means that SQL inserts in table order and complains that you can't write to an Identity field.
Listing the columns (which is always a good practice) solves this:
INSERT INTO tab1 (myColumn1Name, myColumn2Name) VALUES (@col1, @col2)


@OrignalGriff非常感谢您的宝贵意见。

我已经对我的代码做了很多实验,但问题仍未解决,请你提一下如何在vs2008中使用断点调试web方法
@OrignalGriff thanks a lot for your valuable comment.
I have done a lot of experiment on my code but still the problem is not solved, can you please mention how to debug a webmethod with a breakpoint in vs2008


这篇关于无法追踪错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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