简体   繁体   English

在 web 方法中从数据库获取数据返回 vb.net 中的 null

[英]Getting data from database in a webmethod return null in vb.net

I am trying to get data from a stored procedure in a webmethod inside an asmx service but whatever I try it returns null.我正在尝试从 asmx 服务内的 web 方法中的存储过程中获取数据,但无论我尝试什么,它都会返回 null。

<WebMethod()>
<ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=False, 
XmlSerializeString:=False)>
Public Function SendIdDocuments(ByVal idReferto As String)
'This id has been sent from ajax as json and now I am converting it to string 
'in order to use it as parameter in stored procedure.

Dim idRef As String = JsonConvert.DeserializeObject(idReferto)
Dim dtRefertoDocIndex As DataTable = operations.G_REPORT_BY_ID(idRef)
Dim version As String = dtRefertoDocIndex.Rows(0)("VERSION")
MsgBox(Cstr(version))
End If

I have tried these:我试过这些:

1) Dim dtRefertoDocIndex As DataTable = operations.G_REPORT_BY_ID(Cstr(idRef)) 
2) Dim dtRefertoDocIndex As DataTable = operations.G_REPORT_BY_ID(idRef.ToString)

These dont work too.这些也不起作用。 I have checked the idRef that has a value and also data from database isn't returning null and version has a value, but in the code dtRefertoDocIndex is null and for this reason version comes as null too.我已经检查了具有值的 idRef,并且数据库中的数据未返回 null 并且版本具有值,但在代码中 dtRefertoDocIndex 为 null,因此版本也为 null。 Any help would be appriciated.任何帮助将不胜感激。

A few things.一些东西。 Obviously you can't really use msgbox.显然你不能真正使用 msgbox。

Next up, .net should return JSON without you having to specify it, but we can leave that.接下来,.net 应该返回 JSON 而无需指定它,但我们可以保留它。

Your web method WILL automatic split out the parameters for you.您的 web 方法将自动为您拆分出参数。

So, your web method ALSO needs to be a function that RETURNS a value, say a string所以,你的 web 方法也需要是一个 function 返回一个值,比如一个字符串

thus it should be:因此它应该是:

<WebMethod()>
Public Function SendIdDocuments(ByVal idReferto As String) as string

    Dim dtRefertoDocIndex As DataTable = operations.G_REPORT_BY_ID(idRef)
    Dim strResult as string = dtRefertoDocIndex.Rows(0)("VERSION")
    return strResult

End If

Note this part: Public function bla bla bla) as string <---- your function HAS to return something, right?注意这部分:Public function bla bla bla) as string <---- 你的 function 必须返回一些东西,对吧?

Now WHY did I create a string to hold the return value and THEN return it?现在为什么我创建一个字符串来保存返回值然后返回它?

Answer:回答:

Because I do NOT want to take a chance that the return type is a column cell, or ANY OTHER data type OTHER then a string.因为我不想冒险返回类型是列单元格,或者任何其他数据类型 OTHER 然后是字符串。 So, to be 100%, 200%, 300% of the correct data type being returned?那么,要返回 100%、200%、300% 的正确数据类型? I create a nice separate string, and stuff the value into that string, and THEN return that SIMPLE string type.我创建了一个很好的单独字符串,并将值填充到该字符串中,然后返回该 SIMPLE 字符串类型。 This removes ANY doubt that we ARE in fact returning a simple string type.这消除了我们实际上返回一个简单字符串类型的任何疑问。 When we execute the return strResult?当我们执行返回strResult?

It will be converted back into a json result.它将被转换回 json 结果。

Ok, now, lets setup the JavaScript (client side) ajax call.好的,现在,让我们设置 JavaScript(客户端)ajax 调用。

jQuery: jQuery:

        function GetDocByID() {

            var MyDocID = 123;

            $.ajax({
                type: "POST",
                url: 'WebService1.asmx/MakeName',
                data: JSON.stringify({idReferto: MyDocID}),
                contentType: "application/json; charset=utf-8",
                datatype: "json",
                success: function (em) {
                    alert('VERSION result = ' + em.d);
                }
            });
        }

If you not using jQuery, then of course you have to re-write above.如果你不使用jQuery,那你当然要重新写上面。

So, .net will automatic convert to and from json for you.因此,.net 将自动为您转换为 json。 And ALSO VERY careful note how the parameter name in JavaScript (jQuery) MUST match the parameters in the public function you created.还要非常小心地注意 JavaScript (jQuery) 中的参数名称必须如何与您创建的公共 function 中的参数相匹配。

Here is another simple example.这是另一个简单的例子。 We pass firstname, last name, and return the two as a single string:我们传递名字和姓氏,并将两者作为单个字符串返回:

<WebMethod()>
Public Function MakeName(FirstName As String, LastName As String) As String

    Dim strResult As String = ""

    strResult = FirstName & " " & LastName

    Return strResult

End Function

and the js/jQuery is: js/jQuery 是:

        function GetNameTest() {

            var MyFirstName = 'Albert';
            var MyLastName = 'Kallal';

            $.ajax({
                type: "POST",
                url: 'WebService1.asmx/MakeName',
                data: JSON.stringify({ FirstName: MyFirstName, LastName: MyLastName }),
                contentType: "application/json; charset=utf-8",
                datatype: "json",
                success: function (em) {
                    alert('name result = ' + em.d);
                }
            });
        }

Note again HOW we used the actual vb.net FirstName and LastName DEFINED in the public function.再次注意我们如何使用在公共 function 中定义的实际 vb.net 名字和姓氏。

Note again: the function has a return value (string).再次注意:function 有一个返回值(字符串)。

And if you look at the web service template, note that you WANT to un-comment the FIRST line as outlined like this:如果您查看 web 服务模板,请注意您想要取消注释第一行,如下所示:

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM