Use WebService to create a simple train schedule

Source: Internet
Author: User

Train Timetable WebService: http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx

Front-endCode:

Front-end code

 1 <Body> 2 <Form ID = "  Form1  " Runat = "  Server  " > 3 <Div> 4   Origin Site: 5 <Asp: textbox id = "  Txtstartstation  " Runat = "  Server  " > </ASP: textbox> 6 Arrival site: <asp: textbox id = "  Txtarrivatestation  " Runat = "  Server  " > </ASP: textbox> <ASP: button  7 Id = "  Btnsubmit  " Runat = "  Server  " TEXT = "  Search  " Onclick = "  Btnsubmit_click1  " /> 8 <Asp: gridview id ="  Gv  " Runat = "  Server  " Autogeneratecolumns = "  False  " > 9 <Columns> 10 <Asp: boundfield datafield = "  Traincode  " Headertext ="  Vehicle count  " /> 11 <Asp: boundfield datafield = "  Firststation  " Headertext = "  Origin Site  " /> 12 <Asp: boundfield datafield = "  Laststation  " Headertext ="  Terminal  " /> 13 <Asp: boundfield datafield = "  Startstation  " Headertext = "  Departure station  " /> 14 <Asp: boundfield datafield = "  Starttime  " Headertext ="  Departure time  " /> 15 <Asp: boundfield datafield = "  Arrivestation  " Headertext = "  Arrival site  " /> 16 <Asp: boundfield datafield = "  Arrivetime  " Headertext ="  Arrival Time  " /> 17 <Asp: boundfield datafield = "  Km  " Headertext = "  Mileage  " /> 18 <Asp: boundfield datafield = "  Usedate  " Headertext ="  Duration  " /> 19 </Columns> 20 </ASP: gridview> 21 </Div> 22 </Form> 23 </Body>

Background code:

 Public  Datatable createdatatabletype () {datatable dt = New  Datatable (); DT. Columns. Add ( "  Traincode  " , Typeof ( String  ); DT. Columns. Add (  "  Firststation  " , Typeof ( String  ); DT. Columns. Add (  "  Laststation  " ,Typeof ( String  ); DT. Columns. Add (  "  Startstation  " , Typeof ( String  ); DT. Columns. Add (  "  Starttime  " , Typeof ( String  ); DT. Columns. Add ( "  Arrivestation  " , Typeof ( String  ); DT. Columns. Add (  "  Arrivetime  " , Typeof ( String  ); DT. Columns. Add (  "  Km  " ,Typeof ( String  ); DT. Columns. Add (  "  Usedate  " , Typeof ( String  ));  Return  DT ;}  Protected   Void Btnsubmit_click ( Object  Sender, eventargs e ){} Protected   Void Btnsubmit_click1 ( Object  Sender, eventargs e) {train. traintimewebservice ttws = New  Train. traintimewebservice (); dataset DS = Ttws. getstationandtimebystationname ( This . Txtstartstation. Text. Trim (), This . Txtarrivatestation. Text. Trim (), ""  ); Datatable newdt = Createdatatabletype (); If (DS = Null | Ds. Tables. Count <= 0 | Ds. Tables [ 0 ]. Rows. Count <= 0  )  Return  ;  This . Gv. datasource = Ds. Tables [ 0  ];  This  . Gv. databind ();  For (Int I = 0 ; I <Ds. Tables [ 0 ]. Rows. Count; I ++ ) {Datarow Dr = Newdt. newrow (); Dr [  "  Traincode  " ] = Ds. Tables [ 0 ]. Rows [I] [ "  Traincode  "  ]. Tostring (); Dr [  " Firststation  " ] = Ds. Tables [ 0 ]. Rows [I] [ "  Firststation  "  ]. Tostring (); Dr [  "  Laststation  " ] = Ds. Tables [ 0 ]. Rows [I] [ "  Laststation  "  ]. Tostring (); Dr [ "  Startstation  " ] = Ds. Tables [ 0 ]. Rows [I] [ "  Startstation  "  ]. Tostring (); Dr [  "  Starttime  " ] = Ds. Tables [ 0 ]. Rows [I] [ "  Starttime  " ]. Tostring (); Dr [  "  Arrivestation  " ] = Ds. Tables [ 0 ]. Rows [I] [ "  Arrivestation  "  ]. Tostring (); Dr [  "  Arrivetime  " ] = Ds. Tables [ 0 ]. Rows [I] [ "  Arrivetime "  ]. Tostring (); Dr [  "  Km  " ] = Ds. Tables [ 0 ]. Rows [I] [ "  Km  "  ]. Tostring (); Dr [  "  Usedate  " ] = Ds. Tables [ 0 ]. Rows [I] [ " Usedate  "  ]. Tostring (); newdt. Rows. Add (DR );}  Using (Sqlconnection con = New Sqlconnection ( "  Data Source =.; initial catalog = demo; Integrated Security = true  "  ) {Con. open ();  Using (Sqltransaction TRAN = Con. begintransaction ()){  Try {  String Strdel = "  Delete train where traincode in (select traincode from @ DT)  "  ;  String Strsql = "  Insert into train ([traincode], [firststation], [laststation], [startstation], [starttime], [arrivestation], [arrivetime], [km], [usedate]) select [traincode], [firststation], [laststation], [startstation], [starttime], [arrivestation], [arrivetime], [km], [usedate] From @ dt  " ; Sqlcommand cmd = New  Sqlcommand (strdel, con); cmd. Transaction = Tran; sqlparameter sp = New Sqlparameter ( "  @ Dt  "  , Sqldbtype. Structured); sp. typename = "  Typetrain  " ; Sp. Value = Newdt; cmd. Parameters. Add (SP); cmd. executenonquery (); cmd. Parameters. Clear (); cmd. commandtext = Strsql; cmd. Parameters. Add (SP); cmd. executenonquery (); Tran. Commit ();}  Catch  (Exception ex) {Tran. rollback ();}}}} 

Database Table value variable type:

Table value variable type

 1  Create type [DBO]. [typetrain] As table (  2 [Traincode] [nvarchar] ( 50  ) Not null,  3 [Firststation] [nvarchar] ( 50  ) Null,  4 [Laststation] [nvarchar] ( 50  ) Null,  5 [Startstation] [nvarchar] ( 50  ) Null,  6  [Starttime] [datetime] Null,  7 [Arrivestation] [nvarchar] ( 50  ) Null,  8   [Arrivetime] [datetime] Null,  9 [Km] [ Decimal ] ( 18 , 0  ) Null,  10 [Usedate] [time] ( 7  ) Null, 11   Primary Key clustered  12   (  13   [Traincode] ASC  14 ) With (ignore_dup_key = Off)  15   )  16 Go

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.