How to fetch data from URL and display in DataList using C# and VB.Net in ASP.Net

Last Reply on Jul 10, 2017 07:02 AM By Indresh

Posted on Jul 01, 2017 02:31 PM

I want to fetch data from URL and insert into database then display it as a content on page. Like this URL link - 

http://www.elle.com/fashion/news/a46398/every-time-bella-hadid-leg-slit/

Posted on Jul 04, 2017 07:40 AM Modified on on Jul 05, 2017 05:41 AM

Hi micah,

I have created sample code which full-fill your requirement. So please refer the below code and modify as per your requirement.

HTML

<div>
    <asp:DataList ID="dlUrl" runat="server">
        <ItemTemplate>
            <iframe src='<%# Eval("Url")%>'></iframe>
            <asp:HyperLink NavigateUrl='<%# Eval("Url")%>' Text='<%#Eval("description")%>' runat="server" />
        </ItemTemplate>
    </asp:DataList>
</div>

C#

private void GetUrlData()
{
    DataTable dt = new DataTable();
    dt.Columns.Add("Image");
    dt.Columns.Add("Url");
    dt.Columns.Add("description");
    dt.Rows.Add("http://www.aspforums.net/Avatars/micah.jpg?635632963137500000", "http://www.aspforums.net/Threads/174477/How-do-I-fetch-data-from-URL-and-display-it-on-datalist/", "How-do-I-fetch-data-from-URL-and-display-it-on-datalist");
    dt.Rows.Add("http://www.aspforums.net/Avatars/micah.jpg?635632963137500000", "http://www.aspforums.net/Threads/185894/How-do-i-hide-or-diplay-lable-if-status-is-true-on-Modal-without-datalist/", "How-do-i-hide-or-diplay-lable-if-status-is-true-on-Modal-without-datalist");
    dlUrl.DataSource = dt;
    dlUrl.DataBind();
}

Vb.Net

Private Sub GetUrlData()
	Dim dt As New DataTable()
	dt.Columns.Add("Image")
	dt.Columns.Add("Url")
	dt.Columns.Add("description")
	dt.Rows.Add("http://www.aspforums.net/Avatars/micah.jpg?635632963137500000", "http://www.aspforums.net/Threads/174477/How-do-I-fetch-data-from-URL-and-display-it-on-datalist/", "How-do-I-fetch-data-from-URL-and-display-it-on-datalist")
	dt.Rows.Add("http://www.aspforums.net/Avatars/micah.jpg?635632963137500000", "http://www.aspforums.net/Threads/185894/How-do-i-hide-or-diplay-lable-if-status-is-true-on-Modal-without-datalist/", "How-do-i-hide-or-diplay-lable-if-status-is-true-on-Modal-without-datalist")
	dlUrl.DataSource = dt
	dlUrl.DataBind()
End Sub

 Screenshot

 


Posted on Jul 10, 2017 07:02 AM Modified on on Jul 10, 2017 08:12 AM

Hi micah,

I have modified code as per your requirement. So please refer the below code and you need to set height and width as per your requirement.

HTML

<div>
    <asp:TextBox ID="txtUrl" placeholder="Enter your Url" Width="370px" runat="server" />
    <br />
    <asp:TextBox ID="txttitle" placeholder="Enter a Title" Width="370px" runat="server" />
    <br />
    <asp:TextBox ID="txtDescription" placeholder="Enter a Description" Width="370px"
        runat="server" />
    <br />
    <asp:TextBox ID="txtImageLink" placeholder="Image Url" Width="370px" runat="server" />
    <br />
    <br />
    <asp:Button Text="Submit" OnClick="Submit" runat="server" />
    <div>
        <hr />
        <asp:FormView ID="DlAdverties" runat="server" Width="100%">
            <ItemTemplate>
                <div class="box box box-solid direct-chat direct-chat" style="margin-bottom: 8px">
                    <div class="box-header " style="background-color: transparent; margin-bottom: 1px;
                        margin-top: 1px">
                        <div class="" style="margin-top: 2px; font-variant-caps: titling-caps">
                            <div class="">
                            </div>
                            <div class=" " style="">
                                <h3 class="box-title">
                                    <asp:Label ID="Label5" runat="server" Text="" CssClass=" glyphicon glyphicon-map-marker"></asp:Label>
                                    Sponsored <span>
                            </div>
                        </div>
                    </div>
                    <!-- /.box-header -->
                    <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# Eval("URL") %>' CssClass="   ">
                        <%-- <asp:Image ID="Image1" ImageUrl='<%#Eval("Image") %>' runat="server" Width="30%" />--%>
                        <iframe scrolling="no" src='<%# Eval("URL")%>'></iframe>
                        <div class=" well-sm" style="">
                            <asp:Label ID="Label2" runat="server" Text='<%#Eval("Title") %>' Font-Bold="True "
                                Font-Size="" />
                            <div class="clearfix" style="">
                            </div>
                            <asp:Label ID="Label3" runat="server" Text='<%#Eval("Website") %>' Font-Bold="True "
                                Font-Size="Smaller" />
                            <div class="clearfix" style="">
                            </div>
                            <asp:Label ID="Label4" runat="server" Text='<%#Eval("Description") %>' Font-Bold="True "
                                Font-Size="" ForeColor="#999999" />
                            <br />
                        </div>
                    </asp:HyperLink>
                    <div class="panel-footer">
                        <div class="row">
                            <div class="col-sm-12 text-right" style="">
                                <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("URL") %>' CssClass=" btn btn-default  ">
                                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("Category") %>' Font-Bold="True"></asp:Label>
                                </asp:HyperLink>
                            </div>
                        </div>
                    </div>
                </div>
            </ItemTemplate>
        </asp:FormView>
    </div>
</div>

C#

private void GetData()
{
    SqlConnection con = new SqlConnection(constring);
    SqlCommand cmd = new SqlCommand("SELECT * FROM WebLink", con);
    DataTable dt = new DataTable();
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    sda.Fill(dt);
    if (dt.Rows.Count > 0)
    {
        DlAdverties.DataSource = dt;
        DlAdverties.DataBind();
    }
}

protected void Submit(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(constring);
    SqlCommand cmd = new SqlCommand("Insert into WebLink values(@Url,@Image,@Title,@Website,@Description,@Category)", con);
    cmd.Parameters.AddWithValue("@Url", txtUrl.Text.Trim());
    cmd.Parameters.AddWithValue("@Image", txtImageLink.Text.Trim());
    cmd.Parameters.AddWithValue("@Title", txttitle.Text.Trim());
    cmd.Parameters.AddWithValue("@Website", txtUrl.Text.Trim());
    cmd.Parameters.AddWithValue("@Description", txtDescription.Text.Trim());
    cmd.Parameters.AddWithValue("@Category", 1);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    GetData();
}

Vb.Net

Private Sub GetData()
	Dim con As New SqlConnection(constring)
	Dim cmd As New SqlCommand("SELECT * FROM WebLink", con)
	Dim dt As New DataTable()
	Dim sda As New SqlDataAdapter(cmd)
	sda.Fill(dt)
	If dt.Rows.Count > 0 Then
		DlAdverties.DataSource = dt
		DlAdverties.DataBind()
	End If
End Sub

Protected Sub Submit(sender As Object, e As EventArgs)
	Dim con As New SqlConnection(constring)
	Dim cmd As New SqlCommand("Insert into WebLink values(@Url,@Image,@Title,@Website,@Description,@Category)", con)
	cmd.Parameters.AddWithValue("@Url", txtUrl.Text.Trim())
	cmd.Parameters.AddWithValue("@Image", txtImageLink.Text.Trim())
	cmd.Parameters.AddWithValue("@Title", txttitle.Text.Trim())
	cmd.Parameters.AddWithValue("@Website", txtUrl.Text.Trim())
	cmd.Parameters.AddWithValue("@Description", txtDescription.Text.Trim())
	cmd.Parameters.AddWithValue("@Category", 1)
	con.Open()
	cmd.ExecuteNonQuery()
	con.Close()
	GetData()
End Sub