Hi maideen,
Check this example. Now please take its reference and correct your code.
Namespaces
C#
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Net;
using Newtonsoft.Json;
VB.Net
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Net
Imports Newtonsoft.Json
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
ProcessAPI();
}
protected void ProcessAPI()
{
string url = "https://api.mocki.io/v1/b043df5a";
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
string json = (new WebClient()).DownloadString(url);
List<BouncesAndBlocks> res = JsonConvert.DeserializeObject<List<BouncesAndBlocks>>(json);
foreach (var bouncesAndBlock in res)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "INSERT INTO BouncesAndBlock VALUES (@Name,@City)";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@Name", bouncesAndBlock.name);
cmd.Parameters.AddWithValue("@City", bouncesAndBlock.city);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
public class BouncesAndBlocks
{
public string name { get; set; }
public string city { get; set; }
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
ProcessAPI()
End Sub
Protected Sub ProcessAPI()
Dim url As String = "https://api.mocki.io/v1/b043df5a"
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)
Dim json As String = (New WebClient).DownloadString(url)
Dim res As List(Of BouncesAndBlocks) = JsonConvert.DeserializeObject(Of List(Of BouncesAndBlocks))(json)
For Each bouncesAndBlock In res
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "INSERT INTO BouncesAndBlock VALUES (@Name,@City)"
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@Name", bouncesAndBlock.name)
cmd.Parameters.AddWithValue("@City", bouncesAndBlock.city)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
Next
End Sub
Public Class BouncesAndBlocks
Public Property name As String
Public Property city As String
End Class