<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Edwin Aragon</title>
    <description>The latest articles on DEV Community by Edwin Aragon (@edwinoaragon).</description>
    <link>https://dev.to/edwinoaragon</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F454478%2F0ad23b04-84f7-40e6-ac41-4a8691de4587.jpg</url>
      <title>DEV Community: Edwin Aragon</title>
      <link>https://dev.to/edwinoaragon</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/edwinoaragon"/>
    <language>en</language>
    <item>
      <title>How to Send a Complex Type List of Objects to SQL Server</title>
      <dc:creator>Edwin Aragon</dc:creator>
      <pubDate>Sat, 03 Oct 2020 20:19:29 +0000</pubDate>
      <link>https://dev.to/edwinoaragon/how-to-send-a-complex-type-list-of-objects-to-sql-server-266b</link>
      <guid>https://dev.to/edwinoaragon/how-to-send-a-complex-type-list-of-objects-to-sql-server-266b</guid>
      <description>&lt;p&gt;Hey community, I'm back! This time I want to share with you how I solved a problem I recently had while working with SQL Server and C#.&lt;/p&gt;

&lt;h1&gt;
  
  
  The Problem
&lt;/h1&gt;

&lt;p&gt;So a little bit of background to get all of you on the same channel. &lt;br&gt;
For this particular project, I'm using SQL Server and ASP.NET MVC 5. I manage the database connection through ADO.NET. I work mostly through Stored Procedures to let the database handle the load of working with the data. When I needed to modify multiple rows I always ended up sending a comma-separated string with the ids I wanted to work with. &lt;/p&gt;

&lt;p&gt;Here is an example of how I send a list of ids to modify.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;PROCEDURE&lt;/span&gt; 
    &lt;span class="cm"&gt;/*This will contain a comma separated list e.g 1,2,3,4,5 and so on*/&lt;/span&gt;
    &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;idList&lt;/span&gt; &lt;span class="nb"&gt;varchar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;MAX&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;AS&lt;/span&gt;
&lt;span class="k"&gt;BEGIN&lt;/span&gt;
     &lt;span class="c1"&gt;--Do something useful with @idList&lt;/span&gt;
&lt;span class="k"&gt;END&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But this time I needed to send not just the ids but a list of complex type elements. I once worked doing this but in that time I  serialized my complex type to XML in order to be able to send it as a string to the database then once in the database I deserialized the XML (Probably you're wondering why XML and not JSON? Well the SQL Server version I was working with was SQL Server 2008 so no JSON available yet for that version) to a table in order to perform the operations I needed. That sounds like a lot of work right? So I decided to search if there was something else easier to work with. &lt;/p&gt;

&lt;h1&gt;
  
  
  The Solution: Table-valued parameters to the rescue!
&lt;/h1&gt;

&lt;p&gt;While searching for a solution I found this interesting way to send parameters to SQL Server for store procedures. But what are table-valued parameters?&lt;/p&gt;

&lt;p&gt;A Table-valued parameter as its name implies is a special parameter you can send to a stored procedure that can hold a specific structure table like defined by us. Basically, you can send a table, you previously defined, as one single parameter to a store procedure what awesome that is?&lt;/p&gt;

&lt;p&gt;Goodbye to those long list of parameters or id lists sent as strings or complex types converted to XML and then serialized to send them also as strings!&lt;/p&gt;

&lt;h2&gt;
  
  
  How do we use them?
&lt;/h2&gt;

&lt;p&gt;In order to use table-valued parameters we need to complete three steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a User-Defined Table Type.&lt;/li&gt;
&lt;li&gt;Create the stored procedure where we are going to use the table-valued parameter and use the recently created User-Defined Table Type.&lt;/li&gt;
&lt;li&gt;Configure ADO.NET to send the parameter as a special parameter.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's break it down each step. Let's imagine that we are working with a movie's web app where you can assign movies to different movie theaters in a single operation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create a User-Defined Table Type
&lt;/h3&gt;

&lt;p&gt;This is the way we tell SQL Server how is the structure we want to send in the table-valued parameter and it's pretty simple to create one. &lt;br&gt;
Let's see how we need to structure it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TYPE&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;schema&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ColumnName&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Colum&lt;/span&gt; &lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;GO&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;An example could be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TYPE&lt;/span&gt; &lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MovieAssignment&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;MovieId&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;Order&lt;/span&gt; &lt;span class="nb"&gt;TINYINT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;MovieTheaterId&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;GO&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Create the stored procedure where we are going to use the table-valued parameter
&lt;/h3&gt;

&lt;p&gt;Then we need to use the just created User-Defined Table Type in the SP where we want to send the table-valued parameter. The structure is the same as a normal SP.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;PROCEDURE&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;schema&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;@&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;parameter&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;user&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="k"&gt;defined&lt;/span&gt; &lt;span class="k"&gt;table&lt;/span&gt; &lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;READONLY&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;AS&lt;/span&gt; 
&lt;span class="k"&gt;BEGIN&lt;/span&gt;
   &lt;span class="c1"&gt;-- SP's body&lt;/span&gt;
&lt;span class="k"&gt;END&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;An important note here is that we must add the &lt;code&gt;READONLY&lt;/code&gt; keyword after the parameter type to tell SQL Server this is a table-valued parameter.&lt;/p&gt;

&lt;p&gt;Back to our movie's app example, we could define an SP as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;PROCEDURE&lt;/span&gt; &lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PersistMovieAssignments&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;MovieAssignments&lt;/span&gt; &lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MovieAssignment&lt;/span&gt; &lt;span class="n"&gt;READONLY&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;AS&lt;/span&gt; 
&lt;span class="k"&gt;BEGIN&lt;/span&gt;
   &lt;span class="c1"&gt;-- SP's body&lt;/span&gt;
&lt;span class="k"&gt;END&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Configure ADO.NET to send the parameter as a special parameter
&lt;/h3&gt;

&lt;p&gt;The last step to make this work is to configure our call to the SP in the C# code to let it know the parameter we are sending is a special table-valued parameter.&lt;/p&gt;

&lt;p&gt;The first thing we need to take into consideration is that we need to send the parameter as &lt;strong&gt;DataTable&lt;/strong&gt;, and mark the parameter type as &lt;strong&gt;Structured&lt;/strong&gt;, this will tell ADO.NET to send the signal this parameter is a table-valued parameter.&lt;br&gt;
Let's see an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;sqlConnection&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;SqlConnection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="cm"&gt;/*Our connection string*/&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;sqlConnection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
   &lt;span class="cm"&gt;/*Create the SqlCommand object to tell SQL what we want to execute, in our case is a stored procedure.*/&lt;/span&gt;
   &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;sqlCommand&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;SqlCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"dbo.PersistMovieAssignments"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sqlConnection&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;CommandType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;CommandType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StoredProcedure&lt;/span&gt;
   &lt;span class="p"&gt;};&lt;/span&gt;
   &lt;span class="cm"&gt;/*Then we create DataTable we want to send as table-valued parameter*/&lt;/span&gt;
   &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;movieAssignments&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;DataTable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
   &lt;span class="n"&gt;movieAssignments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Columns&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"MovieId"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
   &lt;span class="n"&gt;movieAssignments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Columns&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Order"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
   &lt;span class="n"&gt;movieAssignments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Columns&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"MovieTheaterId"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

   &lt;span class="cm"&gt;/*Add the data to the DataTable object*/&lt;/span&gt;
   &lt;span class="n"&gt;movieAssignments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Rows&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="n"&gt;movieAssignments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Rows&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="n"&gt;movieAssignments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Rows&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="cm"&gt;/*Then we create the SqlParameter object where we are going to assign the DataTable containing the data we want to send to SQL Server*/&lt;/span&gt;
   &lt;span class="n"&gt;SqlParameter&lt;/span&gt; &lt;span class="n"&gt;movieAssignmentsParameter&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sqlCommand&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Parameters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AddWithValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"@MovieAssignments"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;movieAssignments&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="n"&gt;movieAssignmentsParameter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SqlDbType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SqlDbType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Structured&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="cm"&gt;/*Then we just execute our command*/&lt;/span&gt;
   &lt;span class="n"&gt;sqlCommand&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ExecuteReader&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If in our &lt;code&gt;PersistMovieAssignments&lt;/code&gt; store procedure we are returning any kind of value we should store the result of executing the &lt;code&gt;ExecuteReader&lt;/code&gt; method in some variable and process it accordingly to extract the values.&lt;/p&gt;

&lt;h1&gt;
  
  
  Important Notes
&lt;/h1&gt;

&lt;p&gt;I added this section as part of an update to the post as I think is worth it to let you know. There are some limitations and important things we need to be aware of when we are working with table-valued parameters some of these are: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;When using ADO.Net with a table-valued parameter, the order of the columns in the data table you're sending to the database must match the order of the columns defined in the user-defined table type. (Thank you Zohar for this important note).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Table-valued parameters are read-only in Transact-SQL code. You cannot update the column values in the rows of a table-valued parameter and you cannot insert or delete rows. To modify the data that is passed to a stored procedure or parameterized statement in the table-valued parameter, you must insert the data into a temporary table or into a table variable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You cannot use ALTER TABLE statements to modify the design of table-valued parameters.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;if you want to know more about the limitations and about table-valued parameters in general the best place to start is its official &lt;a href="https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/table-valued-parameters#limitations-of-table-valued-parameters"&gt;documentation&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Before I know this exists I always was having a hard time when needed to send multiple rows of data to an SP serializing data into XML and then deserializing it back again in SQL Server as I mentioned before. Although this is not wrong this could lead you to insert bugs in the serialize/deserialize part. I personally found this method much more strong typed which is a good thing because you can catch a lot of bugs at compile time.&lt;/p&gt;

&lt;p&gt;And you? Did you know this exists? Have you ever used this? Have you ever faced the need to send complex type lists of objects to SQL Server before? How did you tackle the problem?&lt;/p&gt;

&lt;p&gt;I hope this can be useful for anyone reading it. If you have any comments please leave it in the comments section down below. I will appreciate it!&lt;/p&gt;

&lt;p&gt;Thank you for reading and see you in the next post!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>sql</category>
      <category>csharp</category>
      <category>database</category>
    </item>
    <item>
      <title>The Evil Is In The Details</title>
      <dc:creator>Edwin Aragon</dc:creator>
      <pubDate>Sat, 05 Sep 2020 20:28:41 +0000</pubDate>
      <link>https://dev.to/edwinoaragon/the-evil-is-in-the-details-4hk6</link>
      <guid>https://dev.to/edwinoaragon/the-evil-is-in-the-details-4hk6</guid>
      <description>&lt;p&gt;Let's be honest who doesn't love to apply new cool features when you know of their existence or explore different things to accomplish the same task? even if they're not truly new and they have existed for a long time but you just have heard about them. That's exactly what happened to me and it give me a valuable lesson. &lt;/p&gt;

&lt;p&gt;Let me introduce myself a little bit. I'm Edwin, I'm from México and I'm a 4 year web developer who loves to learn new things and apply them whenever is possible, I mainly around C#, Javascript, HTML, CSS and SQL. I recently realized that if I want to become better at coding I should start putting extra effort on it, this means to practice more even after work, reading more about web development, work on side projects and start posting web development stuff where I can share my knowledge no matter if it's something simple (like this one). So welcome to my first post I hope you find it useful and easy to understand.&lt;/p&gt;

&lt;p&gt;Let's jump in to the point!&lt;/p&gt;

&lt;h1&gt;
  
  
  The XMLHttpRequest Object
&lt;/h1&gt;

&lt;p&gt;Most of web developers know what this dude does. It helps you to communicate to a sever making AJAX calls to it. Although I had to studied it a while ago to complete a certification I've never used it before in a real project because you know, all the libraries out there makes it easy for you to accomplish this task so why bother?&lt;/p&gt;

&lt;p&gt;This time was different, as part of my training to become a better developer I decided to use vanilla JS whenever possible to understand better how JS works, go to the basics to master them and reinforce my current JS knowledge.&lt;/p&gt;

&lt;p&gt;I went directly to the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest"&gt;MDN docs&lt;/a&gt; to read how to implement a simple call using the XMLHttpRequest object. All I needed was to POST some data into the server and once the call was finished log a Success message in the console if the call was completed successfully or log the error message received if not.&lt;/p&gt;

&lt;p&gt;Until this moment I knew about the onreadystatechanged callback was used to notify the status of the call but I found something else in the docs:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;onreadystatechange&lt;/strong&gt; as a property of the &lt;strong&gt;XMLHttpRequest&lt;/strong&gt; instance is supported in all browsers.&lt;br&gt;
Since then, a number of additional event handlers have been implemented in various browsers (&lt;strong&gt;onload&lt;/strong&gt;, &lt;strong&gt;onerror&lt;/strong&gt;, onprogress, etc.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What catched my attention was the second line, event handlers for load and error cool! Let's read more about them.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;XMLHttpRequest&lt;/strong&gt; provides the ability to listen to various events that can occur while the request is being processed. This includes periodic progress notifications, error notifications, and so forth.&lt;br&gt;
&lt;strong&gt;load&lt;/strong&gt; The transfer is complete; all data is now in the response.&lt;br&gt;
The &lt;strong&gt;error&lt;/strong&gt; event is fired when the request encountered an error.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Nice then I will use them to notify the status of my call!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;strong&gt;NOTE:&lt;/strong&gt;&lt;/strong&gt; Although this was not something new per se it was new for me.&lt;/p&gt;

&lt;h1&gt;
  
  
  The Task
&lt;/h1&gt;

&lt;p&gt;So I proceeded to write my code and ended up with something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const ajaxCall = new XMLHttpRequest();
ajaxCall.addEventListener("load", () =&amp;gt; console.log("Success")});
ajaxCall.addEventListener("error", error =&amp;gt; console.log(error));
ajaxCall.open("POST", localhost:30408/post/valid/url);
ajaxCall.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
ajaxCall.send(JSON.stringify(getPostData()));
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When tried this code the POST was correctly sent and processed by the server and logged in the console.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wualPUIZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8b5dacctwhsb5w3w1u18.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wualPUIZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8b5dacctwhsb5w3w1u18.png" alt="ConsoleSuccess"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then I forced the POST to return a 500 http status code to try my error event listener and send a message to log in the console but my surprise was this.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qtR7sWso--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/19mg3z5ealhats6paag3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qtR7sWso--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/19mg3z5ealhats6paag3.png" alt="ConsoleError"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Wait a minute this shouldn't be happening right? I should see my error message instead of my Success message. After some googling I fount out that I was using the error event in the wrong way and how is that? Well it turns out that the error event is not in charge of listening for the response's status but for network problems instead!&lt;/p&gt;

&lt;p&gt;Let's fake a network problem just pointing the request url to something that doesn't exist and see if our error listener catches it.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QHdR1YZe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/s0dksvw3zsvrxqek5ut8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QHdR1YZe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/s0dksvw3zsvrxqek5ut8.png" alt="NetworkErrok"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Victory! the error event was fired, you can see the &lt;code&gt;console.log(error)&lt;/code&gt; line being executed before the console &lt;code&gt;ERR_NAME_NOT_RESOLVED&lt;/code&gt; error. Here we are printing the object because the error listener receives a &lt;code&gt;ProgressEvent&lt;/code&gt; as callback parameter so our &lt;code&gt;error&lt;/code&gt; variable is of type &lt;code&gt;ProgressEvent&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;At the end I will end up using the &lt;code&gt;onreadystatechange&lt;/code&gt; callback but with something new added to my knowledge just for being curious.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;It's OK to try new ways to do the same things you already know how to (as a simple AJAX call) this way you add some extra valuable knowledge to you and keep you learning, which is always a good thing. So keep finding different ways to complete a task you never know what new thing you will learn this time.&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
