VB.net Get text/string from html element - html

I'm having major trouble trying to get bits of elements and returning the strings.
I have a few exmaples of trying to get the strings and what not but failing hard.
HTML Phrasing is difficult for me to do so help would be appreciated.
Explantion of what I need:
I need to get the strinsg of different elements off this site when entering a IP
http://www.ip-tracker.org/
I need pretty much all the details but into labels or text boxes.
Or this with xml phrasing
http://ip-api.com/xml/8.8.8.8
So here is the exmaple that i've used so far but haven't got far with it.
Exmaple 1:
Dim client As New WebClient
Dim ip As String
Dim city As String
Dim Region As String
Private Function GetIp()
Try
Dim Page As String = client.DownloadString("http://www.ip-tracker.org/locator/ip-lookup.php?ip=82.16.38.43/")
ip = Page.Substring(Page.IndexOf("IP Address:") + 80)
ip = ip.Substring(0, city.IndexOf(" </td") + 30)
TextBox2.Text = ("IP Address: " + ip)
Catch ex As Exception
city = "Unable to lookup"
End Try
Return 0
End Function
To call it:
getViews()

Try xml linq
Imports System.Xml
Imports System.Xml.Linq
Module Module1
Dim url As String = "http://ip-api.com/xml/8.8.8.8"
Sub Main()
Dim query As XElement = XElement.Load(url)
Dim status As String = query.Element("status").Value
Dim country As String = query.Element("country").Value
Dim region As String = query.Element("region").Value
Dim regionName As String = query.Element("region").Value
Dim city As String = query.Element("city").Value
Dim zip As String = query.Element("zip").Value
Dim lat As Double = query.Element("lat").Value
Dim lon As Double = query.Element("lon").Value
Dim timezone As String = query.Element("timezone").Value
Dim isp As String = query.Element("isp").Value
Dim org As String = query.Element("org").Value
Dim _as As String = query.Element("as").Value
Dim subQuery As String = query.Element("query").Value
End Sub
End Module

Related

how to use FIND_IN_SET with dataview.rowfilter in vb.net

I'm using this code to filter my datatable by dataview:
Dim xBlockedAccounts As String = "1,5,7"
Dim xDv_AllAcc As New DataView(MyVar_Dt_Accounts)
xDv_AllAcc.RowFilter = "FIND_IN_SET(AccID," & xBlockedAccounts & ")"
Me.Dgv3.DataSource = xDv_AllAcc.ToTable
but it gives me that:
The expression contains undefined function call FIND_IN_SET().'
how I can use FIND_IN_SET function with Rowfilter of Dataview?
I assumed MyVar_Dt_Accounts was a DataTable. You need to have an array of blocked accounts values. Then the Linq magic.
Private Sub OPCode()
Dim MyVar_Dt_Accounts As New DataTable
Dim xBlockedAccounts = {"1", "5", "7"}
Dim dt = (From row As DataRow In MyVar_Dt_Accounts.AsEnumerable
Select row
Where xBlockedAccounts.Contains(row("AccID").ToString)).CopyToDataTable
Dgv3.DataSource = dt
End Sub
Check first that AccID is really a string in the database and not a number.

Consuming data from webservice with vb.net

Im doing a webform in vb.net I'm consuming a webservice, Which returns me to all the countries
Only have 1 button Enviar that calls the countries.
Imports service_country = WebServiceVB2.country
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim serv_country As New service_country.country '--Create object'
Dim MyDoc As New System.Xml.XmlDocument
Dim MyXml As String = serv_country.GetCountries() '--Execute procedure from webservice'
MyDoc.LoadXml(MyXml) '--Read Myxml and convert to XML'
Dim SymbolText As String = MyDoc.SelectSingleNode("//NewDataSet/Table/Name").InnerText '--select the node'
Label1.Text = SymbolText
End Sub
My question is How can I select all the values that are inside the 'name'.
Actually it only shows one.
For Example:
Thanks in advance.
This was an interesting problem. Since data is coming as a webpage the open bracket was coming as "& l t ;" while the closing bracket was coming as "& g t ;". So these had to be replaced. I used xml linq to get the names :
Imports System.Xml
Imports System.Xml.Linq
Module Module1
Const URL As String = "http://www.webservicex.net/country.asmx/GetCountries"
Sub Main()
Dim doc1 As XDocument = XDocument.Load(URL)
Dim docStr As String = doc1.ToString()
docStr = docStr.Replace(">", ">")
docStr = docStr.Replace("<", "<")
Dim doc2 As XDocument = XDocument.Parse(docStr)
Dim root As XElement = doc2.Root
Dim defaultNs As XNamespace = root.GetDefaultNamespace()
Dim names() As String = doc2.Descendants(defaultNs + "Name").Select(Function(x) CType(x, String)).ToArray()
End Sub
End Module
Using WebUtility
Imports System.Xml
Imports System.Xml.Linq
Imports System.Text
Imports System.Net
Module Module1
Const URL As String = "http://www.webservicex.net/country.asmx/GetCountries"
Sub Main()
Dim xReader As XmlReader = XmlTextReader.Create(URL)
xReader.MoveToContent()
Dim doc As XDocument = XDocument.Parse(WebUtility.HtmlDecode("<?xml version=""1.0"" encoding=""iso-8859-9"" ?>" & xReader.ReadOuterXml))
Dim root As XElement = doc.Root
Dim defaultNs As XNamespace = root.GetDefaultNamespace()
Dim names() As String = doc.Descendants(defaultNs + "Name").Select(Function(x) CType(x, String)).ToArray()
End Sub
End Module

sending email in vb.net in HTML formatted page

I made email form in vb.net it works but i need to send a mail using html format which i made that also works but when i receive mail the value written in text box is not coming in mail. what is problem in this?
Imports System
Imports System.Net.Mail
Imports System.Net
Imports System.IO
Imports System.Configuration
Partial Class Form
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
End Sub
Protected Sub submit_button_Click(sender As Object, e As EventArgs) Handles submit_button.Click
SendHTMLMail()
End Sub
' Method Which is used to Get HTML File and replace HTML File values with dynamic values and send mail
Public Sub SendHTMLMail()
Dim reader As New StreamReader(Server.MapPath("html.html"))
Dim readFile As String = reader.ReadToEnd()
Dim myString As String = ""
myString = readFile
myString = myString.Replace("$$Company_Name$$", "Company_Name")
myString = myString.Replace("$$Contact_Person_Name$$", "Txt_ContactPerson.Text")
myString = myString.Replace("$$Contact_Person_Mobile_No$$", "Txt_Contact_No.Text")
myString = myString.Replace("$$Telephone_No$$", "Txt_TelNo.Text")
myString = myString.Replace("$$Email_Id$$", "Txt_Email.Text")
myString = myString.Replace("$$Registered_Address$$", "Txt_Address.Text")
myString = myString.Replace("$$Year_Of_Commencement_Of_Business$$", "Txt_CommenceYear.Text")
myString = myString.Replace("$$Income_TAX_PAN_No$$", "Txt_IncomeTax.Text")
myString = myString.Replace("$$CST_No$$", "Txt_CSTNo.Text")
myString = myString.Replace("$$VAT_TIN_No$$", "Txt_VatNo.Text")
myString = myString.Replace("$$Type_Of_Business_Entity$$", "RadioButtonList1.SelectedValue.ToString")
myString = myString.Replace("$$Full_Details$$", "")
myString = myString.Replace("$$Business_History$$", "Txt_BusinessHistory.Text")
myString = myString.Replace("$$No_Of_Employees$$", "Txt_NoOfEmployees.Text ")
myString = myString.Replace("$$Annual_Turnover$$", "")
myString = myString.Replace("$$References$$", "")
Dim MyMailMessage As New MailMessage()
MyMailMessage.From = New MailAddress("test#eternalbs.com", "Eternal")
MyMailMessage.To.Add("test#eternalbs.com")
MyMailMessage.Subject = "Eternal"
MyMailMessage.Body = myString.ToString()
MyMailMessage.IsBodyHtml = True
' Dim SMTPServer As New SmtpClient("smtp.gmail.com")
Dim SMTPServer As New SmtpClient()
SMTPServer.Host = "208.91.198.227"
SMTPServer.Port = 587
SMTPServer.Credentials = New System.Net.NetworkCredential("test#eternalbs.com", "admin123")
'SMTPServer.EnableSsl = True
Try
SMTPServer.Send(MyMailMessage)
Response.Write("Successfull")
Catch ex As SmtpException
Response.Write(ex)
End Try
End Sub
End Class
For receving the value of any textbox which user has inserted, the value must not be in double inverted commas. In the SendHTMLmail() function, the second parameter where you want the textbox value to be captured, u just need to remove the double inverted commas
myString = myString.Replace("$$Contact_Person_Name$$", Txt_ContactPerson.Text)
Try this for every textbox whose value u want to capture. Hope it helps.

Mysql & Vb.net 'Invalid Attempt to Read when reader is closed'

I have run into this error while collecting data from a mysql database and placing it into a DataView control... This is my code:
Private Function PopulateActivity()
Dim loginStatement As String = "SELECT * FROM activity WHERE id = #userid"
Dim cmd As MySqlCommand = New MySqlCommand(loginStatement, mainconn)
cmd.Parameters.AddWithValue("#userid", LoggedInUser.ID)
Dim drMyAcount As MySqlDataReader = cmd.ExecuteReader()
Dim rowCount As Integer = 0
Dim rowAmount As Integer = 0
'gets count of rows returned from mysql query'
Using dt As New DataTable
dt.Load(drMyAcount)
rowAmount = dt.Rows.Count
End Using
'adds an entry for each item returned from the mysql query'
Do While rowCount < rowAmount
drMyAcount.Read() 'HERE IS WHERE ERROR OCCURS'
Dim tempDateTime As String = drMyAcount.Item("dateTime")
Dim tempInfo As String = drMyAcount.Item("info")
Dim tempBalChanges As String = drMyAcount.Item("balChange")
Dim tempToFrom As String = drMyAcount.Item("toFrom")
ActivityView.Rows.Add(tempDateTime, tempInfo, tempBalChanges, tempToFrom)
rowCount = rowCount + 1
Loop
drMyAcount.Close()
Return 0
End Function
I am unaware of why this is but it gives me an 'Invalid Attempt to Read when reader is closed' error one the:
drMyAccount.Read()
line...
I would appreciate any help on this topic! Thanks Much...
take out the dt.Load() , and counting of the rows prior to using datareader. DataReader has a built in property of .HasRows
if (drMyAcount.HasRows)
while (drMyAcount.Read())
Dim tempDateTime As String = drMyAcount.Item("dateTime")
Dim tempInfo As String = drMyAcount.Item("info")
Dim tempBalChanges As String = drMyAcount.Item("balChange")
Dim tempToFrom As String = drMyAcount.Item("toFrom")
ActivityView.Rows.Add(tempDateTime, tempInfo, tempBalChanges, tempToFrom)
rowCount = rowCount + 1 //you can still count rows in the loop
Loop
The MSDN documentation does not seem to specify, but apparently the DataTable.Load method closes the given IDataReader when it is done loading the data into the table. So, once you call dt.Load(drMyAcount), the drMyAcount will be closed.

MySQL Data Reader not entering the loop

I have tried many different things but at this point I am stumped.
Public Function GetCust(email As String) As Integer
Dim cs As String = <connection string>
Dim conn As New MySqlConnection(cs)
Dim custId As Integer
Dim cust_query As String = "SELECT entity_id FROM customer_entity WHERE email = '#email'"
Dim com As New MySqlCommand(cust_query, conn)
Dim reader As MySqlDataReader
Try
conn.Open()
com.Parameters.Add("#email", MySqlDbType.String)
com.Parameters("#email").Value = email
reader = com.ExecuteReader
While reader.Read()
custId = reader.GetInt16(0)
End While
MsgBox(custId)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Return custId
End Function
It should be pulling only one column and one row from a table so i should only have one value out of the query but it isn't entering the loop. Seems like its a syntax error on my part but can't seem to find it. Thanks in advance!
You should remove the quotes around the email parameter in the SQL string:
Dim cust_query As String = "SELECT entity_id FROM customer_entity WHERE email = #email"
This will generate SQL that looks like
WHERE email = ''myemail#mydomain.com''
when it should be
WHERE email = 'myemail#mydomain.com'