REST POST - Build a REST Service in Visual Studio 2015 Part 4

Поділитися
Вставка
  • Опубліковано 27 сер 2024
  • In this session we wire up Post to save data to the database

КОМЕНТАРІ • 51

  • @faizshaari6302
    @faizshaari6302 4 роки тому

    Sorry..but I got 1 question here... How can I close the database connection? Inside this video.. You did open the connection inside a constructor... But how can I close the connection sir? Any suggestion on how can I close the connection by the method inside the video?

  • @surensethumadhavan6249
    @surensethumadhavan6249 6 років тому

    Really great videos. Thank you so much for putting these up. I did get an error when connecting to MySql that "The host localhost does not support SSL connections.”". Just needed to add "SslMode=none" to the connection string.

    • @telejonjensen
      @telejonjensen  6 років тому

      Hi Suren,
      Thanks for the tip and for watching!
      Jon

  • @user-lj2fz1tf7r
    @user-lj2fz1tf7r 7 років тому +1

    Hi,can you help me?
    When I run the program, there is an exception about " System.NullReferenceException "
    Can you tell me what can I do about this? I have been searched for many ways to solve the problems,but there is no useful to it.

    • @telejonjensen
      @telejonjensen  7 років тому

      Hi,
      On what line of code is it throwing the exception?
      Jon

    • @user-lj2fz1tf7r
      @user-lj2fz1tf7r 7 років тому

      The line of code is to insert data to MySQL

    • @telejonjensen
      @telejonjensen  7 років тому +1

      Hi,
      Make sure all the values you are sending to the database are populated.
      Jon

    • @MrAnthony59282
      @MrAnthony59282 6 років тому +1

      Hi i have the same problem, did u resolve it ??

  • @deduu10
    @deduu10 4 роки тому

    Hello Jon,
    thanks fo a really nice series of tutorial on REST.
    I tried to access the web server from another device using the IP address instead of localhost. But, it returns error 400 invalid hostname.
    I wonder if you could share a workaround for this issue?
    Thanks

  • @Paintballcrazy1000
    @Paintballcrazy1000 7 років тому +1

    Your videos are awesome, thanks for sharing! I do have 1 question, I've been using MS SQL Server as my DB engine and now moving towards MySQL - So my question is, should the connection to the database be closed after the POST has been executed? When using MS SQL, I used to close the connection, or I wrapped the code inside a using statement which automatically closes the connection when done. Thanks for the help! :)

    • @telejonjensen
      @telejonjensen  7 років тому +1

      Hi Oerman,
      Yes you should close the connection. If you use connection pooling you will cache connections so that the performance hit on opening and closing connections all the time is less of an impact.
      Thanks for watching!
      Jon

    • @Paintballcrazy1000
      @Paintballcrazy1000 7 років тому

      Jon Jensen perfect thanks very much for clearing up for me :D happy coding

  • @theafricanwalkchannel4302
    @theafricanwalkchannel4302 6 років тому

    please i am getting an unhadeled exception at the command.executenonquery and it says that connection should be vaid and open, but i have opened it according to the steps you gave.

    • @telejonjensen
      @telejonjensen  6 років тому

      Hi Lois,
      You will need to test your connection. Unless you have your database set up exactly like mine - especially with user accounts it will be different. If you are using MySQL use MySQL Workbench to verify you can connect to the database.
      I hope that helps - thanks for watching!
      Jon

  • @MegaThomas123456789
    @MegaThomas123456789 5 років тому

    Hey, I followed the video but when i start debugging it sais: "Column count doesn´t match value count at row 1" any idea why? It happens when i try to execute cmd.ExcecuteNonQuery();

    • @MegaThomas123456789
      @MegaThomas123456789 5 років тому

      Alright I fixed it by changing the sqlString to the same but then using instead of the VALUE('" + personToSave.LastName... etc., I used VALUE(@FN, @LN... etc., then after cmd = new MyS... etc. Add: cmd.Paramaters.AddWithValue("@FN", personToSave.FirstName);... etc.

    • @telejonjensen
      @telejonjensen  5 років тому

      Hi Thomas,
      Thanks for clarifying and for watching!
      Jon

  • @khalidosama4109
    @khalidosama4109 6 років тому

    great tutorial

    • @telejonjensen
      @telejonjensen  6 років тому

      Hi Khalid,
      Thanks for watching!
      Jon

    • @khalidosama4109
      @khalidosama4109 6 років тому

      Jon Jensen
      its really great taturial .. hope give example for uploading file using api .. and taturial for mvc .. thanks a lote

  • @eduardorios7046
    @eduardorios7046 7 років тому

    Hi,
    When I went to post, I was following step by step in Visual Studio, but after the line "cmd.ExecuteNonQuery();". it appeared an invalid operation dialog with this text "Connection must be valid and open."

    • @telejonjensen
      @telejonjensen  7 років тому

      Hi Eduardo, Thanks for watching! It sounds like your database connection did not connect properly. You may have an issue with your database connection string or database. I assume you are using a local database?
      Jon

    • @eduardorios7046
      @eduardorios7046 7 років тому

      Yes, I'm using a local database. What datatype should I use for ID in the database? Because I tested the database connection to Visual Studio and it works well.

    • @telejonjensen
      @telejonjensen  7 років тому

      Hi Eduardo, I am using an int set to auto increment with a primary key. Here is the SQL:
      tblpersonnelIDCREATE TABLE `tblpersonnel` (
      `ID` int(11) NOT NULL AUTO_INCREMENT,
      `FirstName` varchar(50) DEFAULT NULL,
      `LastName` varchar(45) DEFAULT NULL,
      `PayRate` decimal(10,2) DEFAULT NULL,
      `StartDate` datetime DEFAULT NULL,
      `EndDate` datetime DEFAULT NULL,
      PRIMARY KEY (`ID`),
      UNIQUE KEY `ID_UNIQUE` (`ID`)
      ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
      Hope that helps!
      Jon

    • @eduardorios7046
      @eduardorios7046 7 років тому

      Hi Jon,
      That was the problem, I didn't set auto increment, it works now.
      I just started to learn REST services. To be honest, it is confusing when it's your first time, but you made it easier.
      Thanks for sharing.

    • @telejonjensen
      @telejonjensen  7 років тому

      Hi Eduardo, I'm glad you got it working! Keep learning!
      Jon

  • @jmonge075
    @jmonge075 7 років тому

    Hi, can you help me?
    when a do
    response.Headers.Location = new Uri(Request.RequestUri, string.Format("Admin/{0}", id));
    i get
    localhost:55129/api/Admin/Admin/1
    when a do
    response.Headers.Location = new Uri(Request.RequestUri, string.Format("/{0}", id));
    i get
    localhost:55129/api/1
    it should be
    localhost:55129/api/Admin/1
    any idea?

    • @telejonjensen
      @telejonjensen  7 років тому

      Hi Jorge, Is this with the examples in the videos or a general question? Thanks for watching!
      Jon

    • @jmonge075
      @jmonge075 7 років тому

      Jon Jensen, hi, in this example, you have the same issue, but I did not see how do you fix it.
      thanks for the answer

    • @jmonge075
      @jmonge075 7 років тому

      it happens in this video

    • @telejonjensen
      @telejonjensen  7 років тому

      This is taking the value of id and inserting it in place of {0}.

    • @jmonge075
      @jmonge075 7 років тому

      but I get admin/admin/id instead of admin/id

  • @gumbystern
    @gumbystern 6 років тому

    Hi Jon,
    great videos to learn from, really good.
    i am using Microsoft sql server, what can i use in place of "LastInsertedId;" as SQL Server does not have that, i am trying cmd.ExecuteScalar(); but i am getting a null reference exception, but my data is still going in my database.
    also can you please do a video on paging when you retrieve all records from a database, thank you Jon.

    • @gumbystern
      @gumbystern 6 років тому

      It's ok, i got it... great videos Jon.

    • @telejonjensen
      @telejonjensen  6 років тому

      Hi,
      I switch over to using SQL Server in this video: ua-cam.com/video/PJHyjBrGI-g/v-deo.html.
      I'll look at doing one on paging.
      Thanks for watching!
      Jon

  • @I_am_Jek
    @I_am_Jek 5 років тому

    Problems with Comma and Dot. I assume it is because my language is set to German. But if you write a double like 32.12 in the querryString it becomes 32,12 (notice the dot becomes a comma). This led to some confusion since the database treats these now as two sepperate values and therefore generates an error "column count doesn't match value count at row n".
    Maybe this helps someone.
    Cheers
    Nice video series btw.

    • @telejonjensen
      @telejonjensen  5 років тому +1

      Hi Jek,
      Thanks for posting that and for watching!
      Jon

  • @brunomiguelbessaferreira9409
    @brunomiguelbessaferreira9409 6 років тому

    Could you make this POST request with xml formated object? I'm getting null in the value parameter of My Controller
    Thank you in advance and good series about REST :)
    Best regards

    • @telejonjensen
      @telejonjensen  6 років тому

      Hi Bruno,
      Yes you can. Where are you getting the null? When you examine the data?
      Thanks for watching.
      Jon

    • @brunomiguelbessaferreira9409
      @brunomiguelbessaferreira9409 6 років тому

      I decided to change my android app client to consume using JSON anyway, the only problem I got is that I'm unable to call the service by IP.

    • @telejonjensen
      @telejonjensen  6 років тому

      Hi Bruno,
      I did a Android Client using Spring REST that might help: ua-cam.com/video/cSsNI01Py3Q/v-deo.html
      Thanks
      Jon

    • @brunomiguelbessaferreira9409
      @brunomiguelbessaferreira9409 6 років тому

      thank you for your answer once again, anyways I managed to solve the problem :D
      All I had to do was add some bindings to the IIS configuration file, allow some ports on the firewall and it works as a perfect machine.
      Thank you for your time.
      Keep the good videos.
      Very informational and easy to understand.

    • @telejonjensen
      @telejonjensen  6 років тому

      Hi Bruno,
      Glad you got it working!
      Thanks again for watching.
      Jon

  • @NiceTechViews5403
    @NiceTechViews5403 7 років тому

    Hi Jon 5:51 i translated your example to vb.net :
    "POST: api/openWhere
    Public Function PostValue( ByVal value As myDevice) As HttpResponseMessage
    Dim x As String = value.data
    Dim response As New HttpResponseMessage
    response = Request.CreateResponse(HttpStatusCode.Created)
    response.Headers.Location = New Uri(Request.RequestUri, String.Format("resultSet/{0}", x))
    Return response
    End Function
    really working fine and give this ...in the result set i get exact the data i expect to get: localhost:50418/api/resultSet/5C:CF:7F:B3:03:6E;-46;11;9c:b6:d0:17:e3:a9;WLAN-4F8C20%0A5C:CF:7F:B3:03:6E;-46;11;9c:b6:d0:17:e3:a9;WLAN-4F8C20%0A5C:CF:7F:B3:03:6E;-45;11;9c:b6:d0:17:e3:a9;WLAN-4F8C20%0A5C:CF:7F:B3:03:6E;-45;11;9c:b6:d0:17:e3:a9;%0A5C:CF:7F:B3:03:6E;-45;11;9c:b6:d0:17:e3:88;"
    i was looking arround for some hrs now ..and THIS example seem to be my solution..
    For what i was looking for?
    the client application should fire some simple SQL ( via this Post ) to this VS 2017 Rest Application. i retrieve this data from the DB..And send the result back via a response-Message.
    Exactly like done in your example!!
    Now i am wondering if this is ok from a Rest-API-design-perspective?. I get the ResultMessage in "location"!?
    here i am swimming a bit if from a design perspective i am on the right trail
    regards
    kristina

    • @telejonjensen
      @telejonjensen  7 років тому +1

      Hi Kristina,
      If you mean sending SQL from the client I would not recommend it as it could leave you vulnerable to a SQL Injection attack. I'm working on a more complex example.
      The purpose of setting the value in the in the Header is to give pack the auto-generated ID.
      I hope this helps. Thanks for the question and thanks for watching!
      Jon

    • @NiceTechViews5403
      @NiceTechViews5403 7 років тому

      Hi Jon - sure u´r right...injection and stuff...ive done it the other way..i created a new Rest controller in VB.net for the URL and task...i guess this is the proper way ( hope so ;- ) )...the application is very robust the moment ...this is fine the moment

    • @telejonjensen
      @telejonjensen  7 років тому

      Hi,
      I'm glad it is working for you. Thanks!
      Jon