@jmcilhinney
Thanks for providing the code to trim off the quotes and as suggested i am processing the trimming before doing the ado.net.
I do agree that its not a proper csv... its coming that way when i am exporting it from sql server. I can directly pull it from the sql server which i am doing to do afterward just wanted to keep both options open just in case to import from csv and sql directly.
@Mickroy
Thank you for providing the split function i have tried it and it works. However, after using the trim process its not required but i will keep it just in case i might not have write access to csv file.
Here's the code which is working fine except for a glitch :-
I am getting this error "Conversion from type 'DBNull' to type 'String' is not valid" when a telephone is empty... is there any functional i can use to mark them and put it in a text file? like if "telSMS" is empty write on new text file and keep processing the loop?Code:Imports System.Data Imports System.IO Imports System.Data.OleDb Public Class Form1 Dim phlocation As String Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Check if the file location is empty If String.IsNullOrEmpty(phlocation) = True Then MsgBox("CSV location is not provided. Please provide its location!") 'If its not empty then go ahead with next phase Else 'Divide the dir and filename for ado.net connection and cmd strings. Dim fileDir = Path.GetDirectoryName(phlocation) Dim fileNam = Path.GetFileName(phlocation) 'Remove the quotes "" from the csv if any before ado.net process. Thanks jmcilhinney Dim lines As String() = IO.File.ReadAllLines(phlocation) For index As Integer = 0 To lines.GetUpperBound(0) lines(index) = lines(index).Trim("""") Next IO.File.WriteAllLines(phlocation, lines) 'Process ado.net for csv Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Text;Data Source=" & fileDir & "" Dim objConn As New OleDbConnection(sConnectionString) objConn.Open() Dim objCmdSelect As New OleDbCommand("SELECT * FROM " & fileNam & "", objConn) Dim objAdapter1 As New OleDbDataAdapter objAdapter1.SelectCommand = objCmdSelect Dim objDataset1 As New DataSet objAdapter1.Fill(objDataset1, "PhoneBook") DataGridView1.DataSource = objDataset1.Tables(0).DefaultView For i = 0 To objDataset1.Tables(0).Rows.Count - 1 Dim nameSMS As String = objDataset1.Tables(0).Rows(i).Item(0) Dim telSMS As String = objDataset1.Tables(0).Rows(i).Item(1) 'Geting this error if tel is empty "Conversion from type 'DBNull' to type 'String' is not valid." MsgBox(nameSMS & " " & telSMS) 'Use the below code if csv is not striped of quotes """ 'Dim MyValues() As String = cResult.Split(","c) ' Debug.Print("Name = " & MyValues(0)) ' Debug.Print("Tel = " & MyValues(1)) Next i objConn.Close() End If End Sub Private Sub ImportToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImportToolStripMenuItem.Click OpenFileDialog1.Filter = "CSV Files (*.csv)|*.csv" If OpenFileDialog1.ShowDialog() = DialogResult.OK Then phlocation = (OpenFileDialog1.FileName) End If End Sub End Class
Thats the code block i am refering to.HTML Code:For i = 0 To objDataset1.Tables(0).Rows.Count - 1 Dim nameSMS As String = objDataset1.Tables(0).Rows(i).Item(0) Dim telSMS As String = objDataset1.Tables(0).Rows(i).Item(1) 'Geting this error if tel is empty "Conversion from type 'DBNull' to type 'String' is not valid." MsgBox(nameSMS & " " & telSMS) Next i




Reply With Quote