 |
|
|
 |
 |
|
มีครับตัวอย่าง แต่เดินมาดูที่โต๊ะผมเอง
หรือจะทำตามวิธีด้านล่างก็ได้
1.add web reference จาก url ของ web service
2.ตั้งชื่อ web service จากนั้นเราจะเห็น web service นั้นเป็น classๆ หนึ่งใน project ของเรา
3.เวลาใช้ก็เรียกใช้งาน class ผ่าน object ได้ตามปกติ
|
 |
 |
 |
 |
Date :
2010-04-22 12:31:01 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
บังเอิญไม่ค่อยเก่ง ASP.Net พองูๆปลาๆ อยากดูตัวอย่างที่เอาข้อมูลไป Show ใน DataView ต่างไหมคับ หรือเข้าไปดูได้ที่ไหนคับ
|
 |
 |
 |
 |
Date :
2010-04-22 12:39:40 |
By :
tineebel |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ต้องถามก่อนว่ามีพื้นฐานขนาดไหน เคยเขียนโปรแกรมแบบ oop ไหมครับ
webservice มันก็เหมือนเราสร้าง class ขึ้นมาใช้งานธรรมดานี้แหละ
แต่เป็น class ที่ app อื่นๆ สามารถเรียกใช้งานมันได้ด้วย แค่นี้เอง
ส่วนจะเรียกใช้งานหรือจะเอาไปแสดงผลที่ไหนนั้น ก็แล้วแต่นะออกแบบครับ
|
 |
 |
 |
 |
Date :
2010-04-22 13:00:07 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แค่อยากเอาข้อมูล Show ในตารางหน้า web นะครับ
|
 |
 |
 |
 |
Date :
2010-04-23 09:24:07 |
By :
tineebel |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้า add web reference ได้แล้วก็ทำเหมือนปกตินั่นแหละครับ
โปรโมชั่นสำหรับมือใหม่เลยนะ ปกติจะไม่ค่อยให้โค้ด
--------------------------------------------------------------------------
เมื่อ add web reference เสร็จแล้ว สมมุติว่าตั้งชื่อเป็น MyWebService เวลาใช้ก็ต้อง include namespace ก่อน
Code (C#)
using MyWebService; //ชื่อที่เราตั้งให้ web service นั้น
สมมุติอีกว่า namespace MyWebService ของเรามี class ชื่อ MyService ซึ่งมี method ให้ใช้ คือ public int Add(int x, int y); เวลาจะใช้เราก็ต้องเรียกใช้ผ่าน object โดยสร้างเป็น object ขึ้นมา
Code (C#)
MyService theService = new MyService(); //สร้าง object จาก class
int result = theService.Add(1, 1); //เรียกใช้ method Add จาก theService
Label Label1 = new Label();
Label1.Text = result.ToString(); //เอาค่าใน result ไปแสดงผลที่ label
Page.Form.Controls.Add(Label1); //add Label1 ลงในบน Page
เป็นไง asp.net เหมือนเขียนเว็บไหมครับ อิอิ
|
 |
 |
 |
 |
Date :
2010-04-23 09:44:18 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code ของผมที่ดึงข้อมูล ต้องปรับตรงไหนไหมคับ เพื่อเอาข้อมูลขึ้นตารางทำได้แค่ดึงข้อมูลออกมาแสดงกับกรองข้อมูล
Code (VB.NET)
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim customerPageURL As String = "http://localhost:7047/DynamicsNAV/WS/CRONUS International Ltd/Page/Customer"
Dim customerService As New CustomerPageRef.Customer_Service()
customerService.Url = customerPageURL
customerService.UseDefaultCredentials = True
Dim customer10000 As CustomerPageRef.Customer = customerService.Read("10000")
Response.Write(vbCrLf + "Name of Customer 10000: " + customer10000.Name + "<Br>")
Dim filter1 As New CustomerPageRef.Customer_Filter()
Dim filter2 As New CustomerPageRef.Customer_Filter()
Dim filters() As CustomerPageRef.Customer_Filter = New CustomerPageRef.Customer_Filter(1) {filter1, filter2}
Dim customers() As CustomerPageRef.Customer = customerService.ReadMultiple(filters, Nothing, 0)
For Each customer As CustomerPageRef.Customer In customers
Response.Write("Customer Name : " & customer.Name + "<BR> ")
Next
End Sub

|
 |
 |
 |
 |
Date :
2010-04-23 11:30:29 |
By :
tineebel |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เวลาดูโค้ดคนอื่นที่เขียนแบบไม่เป็นมาตรฐานนี่งงจริงๆ ซึ่งผมก็รู้หรอกนะว่าคุณ add web reference ยังไง
ทำไมต้องมีป้อน url ทีหลังด้วย ผมอ่านโค้ดคุณรู้เรื่องนะ แต่ศึกษาการใช้ web service ใหม่เถอะ
ขอร้องเอาแบบที่คนธรรมดาเขาเขียนกันน่ะ มันง่ายกว่านี้ตั้งเยอะ
Code (VB.NET)
Dim customerService As New CustomerPageRef.Customer_Service()
'CustomerPageRef = namespace
'Customer_Service() = class
จากนั้นคุณก็สร้าง pointer ให้ชี้ไปที่ข้อมูล customer อะไรก็ไม่รู้ 10000 มาไว้ใน customer10000 โดยมี type เป็น CustomerPageRef.Customer เสร็จแล้วก็เอามาแสดงผล
Code (VB.NET)
Dim customer10000 As CustomerPageRef.Customer = customerService.Read("10000")
Response.Write(vbCrLf + "Name of Customer 10000: " + customer10000.Name + "<Br>")
ส่วนอันล่างนี่ก็สร้าง object จาก class Customer_Filter() 2 ชุด เก็บลง array เพื่อลงไปให้ method ReadMultiple -_-" ซึ่งจะ return กลับมาเป็น customers array จากนั้นก็เอาไปวนลูปแสดงผล
Code (VB.NET)
Dim filter1 As New CustomerPageRef.Customer_Filter()
Dim filter2 As New CustomerPageRef.Customer_Filter()
Dim filters() As CustomerPageRef.Customer_Filter = New CustomerPageRef.Customer_Filter(1) {filter1, filter2}
Dim customers() As CustomerPageRef.Customer = customerService.ReadMultiple(filters, Nothing, 0)
For Each customer As CustomerPageRef.Customer In customers
Response.Write("Customer Name : " & customer.Name + "<BR> ")
Next
ส่วนตารางที่ว่านี่ใช่ gridview หรือเปล่า มันมี property ชื่อ datasource อยู่
แต่คุณจะเอา customers ของคุณไปเป็น source มันไม่ได้แน่ๆ เพราะมันคงไม่รับ object แบบเฉพาะนี้
คุณเล่น return มาเป็น type customer เลยแบบนี้ gridview ไม่รู้จักหรอกครับ
ดังนั้นถ้า web service นี้คุณเป็นคนออกแบบเอง ถ้าสามารถแก้ไข
ให้มัน return ค่าเป็น type dataset datatable ก็แก้ซะ จะได้เอาไป bind ให้ gridview มันแสดงผลได้ง่ายๆ
จะใช้งานอย่างอื่นดูง่าย แค่อ้างอิงตำแหน่งให้ถูกเท่านั้น
web service มันก็ class ธรรมดาๆ นี่แหละ เพียงแต่สามารถใช้งานข้าม app ได้ จะเขียนให้ใช้ง่ายก็ได้
จะเขียนให้ยุ่งยากก็ได้ ก็แล้วแต่จะออกแบบนะ เพราะเป็นผมเขียนใหม่ไปแล้ว
|
 |
 |
 |
 |
Date :
2010-04-23 12:22:26 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มาสงสัยต่อ webservice มันส่ง type customer มาได้ด้วยเหรอ งงมากมาย
แค่ผมส่ง dataset ไป jsp มันยังไม่รู้จักเลย ต้องส่งเป็น type มาตรฐาน เช่น string int หรือ array แบบนี้
|
 |
 |
 |
 |
Date :
2010-04-23 12:39:56 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
รับทราบครับ ขอบคุณมาก แต่เรื่อง customers object นี้แปลงไปยังไงดีครับ เพื่อเอาใส่ gridview ได้ เพราะผมไม่สามารถแก้ไข object ใน Webservice ได้คับ
|
 |
 |
 |
 |
Date :
2010-04-23 12:43:44 |
By :
tineebel |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Dim Dt As New System.Data.DataTable()
Dim Dc As New System.Data.DataColumn()
Dc.DataType = System.Type.GetType("System.string")
Dc.ColumnName = "Name"
Dt.Columns.Add(Dc)
For Each customer As CustomerPageRef.Customer In customers
Dim Dr As System.Data.DataRow = Dt.NewRow()
Dr("Name") = customer.Name
Dt.Rows.Add(Dr)
Next
Dim GridView1 As New GridView()
GridView1.DataSource = Dt
GridView1.DataBlind()
Page.Form.Controls.Add(GridView1)
|
 |
 |
 |
 |
Date :
2010-04-23 13:14:41 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากครับ
|
 |
 |
 |
 |
Date :
2010-04-23 15:20:47 |
By :
tineebel |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สอบถามต่อครับ
ถ้าผมอยากให้ ขอมูลที่ได้มาจาก webservice ที่บันทึกลงใน database เท่ากับจำนวน field ที่ได้มาจาก webservice ผมต้องเขียน Code ยังไงครับ
|
 |
 |
 |
 |
Date :
2010-05-13 11:28:07 |
By :
tineebel |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Dim Dt As New System.Data.DataTable()
Dim Dc As New System.Data.DataColumn()
Dc.DataType = System.Type.GetType("System.string")
Dc.ColumnName = "Name"
Dt.Columns.Add(Dc)
For Each customer As CustomerPageRef.Customer In customers
Dim Dr As System.Data.DataRow = Dt.NewRow()
Dr("Name") = customer.Name
Dr("Field1") = customer.Field1
Dr("Field2") = customer.Field2
Dr("Field3") = customer.Field3
Dt.Rows.Add(Dr)
Next
Dim GridView1 As New GridView()
GridView1.DataSource = Dt
GridView1.DataBlind()
Page.Form.Controls.Add(GridView1)
จากนั้นก็เอา datatable ไปดัมพ์ลง database โดยใช้ dataadapter เป็นตัวกลางในการจัดการ
|
 |
 |
 |
 |
Date :
2010-05-13 17:13:45 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ
|
 |
 |
 |
 |
Date :
2010-05-14 09:35:38 |
By :
tineebel |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|