Unwanted data truncating from Excel - sql

When I try to do an import from an Excel document the comments get truncated. I have checked the usually issue that the Table would be limited but is set as:
Comments ... nvarchar(MAX)
Sample of the code, please note even running the code in Debug mode I can see the parameter is truncated before it even goes to the stored procedure.
Dim excelConnectionString As String = (Convert.ToString("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=") & vFileNameFolder) + "; Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';"
'#### Upload, Rename and save file
'#### Open Excel to Parse here
Dim ds As New DataSet
Dim oleda As New OleDbDataAdapter()
Dim cmdExcel As New OleDbCommand()
'#### End - Open Excel to Parse here
Dim vActionRef As String = ""
Try
Dim excelConnection As New OleDbConnection(excelConnectionString)
With cmdExcel
.CommandText = "Select * from [Portal$A1:BB9999]" 'Names we want to select and the name of the sheet
.CommandType = CommandType.Text
.Connection = excelConnection
End With
excelConnection.Open()
oleda = New OleDbDataAdapter(cmdExcel)
oleda.Fill(ds, "dataExcel")
If ds.Tables("dataExcel").Rows.Count > 0 Then
'#### Stored procedure details
Dim connection As SqlConnection
Dim commandSQL As New SqlCommand
Dim FRAUPRN As String = ""
Dim ConnectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("SQLLocal").ToString()
'########### End - Stored procedure details
'Set date once
Dim vDate As Date
vDate = DateTime.Now.AddDays(0)
connection = New SqlConnection(ConnectionString)
connection.Open()
'Dims for error handling and checking for invalid characters
Dim iImported As Integer
For j As Integer = 0 To ds.Tables("dataExcel").Rows.Count - 1 ' counted rows so loop through, ignores first row with names in
If (IsDBNull(ds.Tables("dataExcel").Rows(j)("UPRN"))) Then
'skip
Else
iImported = iImported + 1
'Bring the data across, the rows(i)("xxx") must match a name on the Excel sheet but DOES NOT have to be in order
With commandSQL
.Parameters.Clear()
.Connection = connection
.CommandText = "spAddCSVDataLine" 'Stored procedure here
If Trim(ds.Tables("dataExcel").Rows(j)("Comments")) = "0" Then
.Parameters.AddWithValue("Comments", " ")
Else
' .Parameters.AddWithValue("Comments", If(IsDBNull(ds.Tables("dataExcel").Rows(j)("Comments")), "", Trim(ds.Tables("dataExcel").Rows(j)("Comments"))))
Dim vComments As String
vComments = ds.Tables("dataExcel").Rows(j)("Comments")
.Parameters.AddWithValue("Comments", vComments)
Session.Item("Comments") = Session.Item("Comments").ToString & "//" & vComments
End If
I have looked at similar questions such as ADO is truncating Excel data which talks about numerical issues but am struggling to find the reason why I am losing data before I export the data. 'Common sense' says excel is not passing over more than 255 characters but then this is programming!

I've had all sorts of problems with the JET/Ace DB engine truncating and doing other sorry-ass guesses at data type. Check out this Microsoft article that talks a bit about how JET uses only the first 8 records to determine field length (http://support.microsoft.com/kb/189897/en-us). You can edit a registry setting to change how many records it will scan to determine field length, but the results still seem to be hit or miss for folks.
You might also find some luck in creating a dummy record at the top of the excel sheet that contains a comment with the maximum number of characters of any of your comments. Then just delete that one record after it comes through. Again... results seem to be mixed here.

Related

SQL query table name syntax error through ODBC connnection using VB.Net

I have an SQL query that I'm trying to write to obtain information from an Access table. I'm using Visual Studio 2019 and writing in VB.Net. I have had no issue establishing my ODBC connection, but my SQL string is giving me an error
System.Data.Odbc.OdbcException: 'ERROR [HY000] [DataDirect][ODBC Progress OpenEdge Wire Protocol driver][OPENEDGE]Syntax error in SQL statement at or about "[item-desc] FROM PUB.item" (10713)'
Right now I'm just trying to grab a piece of information from a specific column, but if I use a wildcard as my SELECT the query works. The Access table has a column named item-desc that I'm trying to grab from, but I also tried one called key just to see if it was an issue with the - character. I've tried different formatting with the brackets, quotes adding table names, etc but I have gotten the same error each time. Any insight would be appreciated; I have been unable to find any further information regarding this specific circumstance. Thanks!
Public Sub MySQLConnect() ' Open ODBC Connection for SQL query
Dim Conn As OdbcConnection
Conn = New OdbcConnection("DRIVER = Progress OpenEdge 12.1 Driver;PWD=XXXXXXX;KeepAlive=0;LoadBalanceTimeout=0;FailoverPreconnect=0;FailoverGranularity=0;FailoverMode=0;ValidateServerCertificate=1;EncryptionMethod=0;ConnectionRetryDelay=3;ConnectionRetryCount=0;LoadBalancing=0;DefaultLongDataBuffLen=2048;QueryTimeout=0;LoginTimeout=15;ArraySize=50;DefaultIsolationLevel=1;EnableTimestampWithTimezone=1;UseWideCharacterTypes=0;StaticCursorLongColBuffLen=4096;LogonID=TESTING;Database=demo;PortNumber=5555;HostName=192.168.000.000;Description=Encompix Demo;WorkArounds2=8192;FILEDSN=C:\Program Files\Common Files\ODBC\Data Sources\EncompixDemo.dsn")
Dim SQLString As String = "SELECT [item-desc] FROM PUB.item" 'WHERE [key] = 196" ' Add table name, search parameters, CADLink variable
Dim cmd As OdbcCommand = New OdbcCommand(SQLString, Conn)
Dim Reader As OdbcDataReader
Dim ColumnCount As Integer
Dim output As String
Dim data As Object() = New Object(10) {}
Conn.Open()
MsgBox("Connected, baby!")
Reader = cmd.ExecuteReader()
While Reader.Read()
ColumnCount = Reader.GetValues(data)
output = ""
For i As Integer = 0 To ColumnCount - 1
output = output & " " & data(i)
Debug.WriteLine(data(i))
Next
'Debug.WriteLine(output)
End While
Conn.Close()
End Sub```

out of range error in vb.net and access get maxnumber

iam trying to get the max number from table and insert it to another number
with vb.net 2008 and access db 2003 this my code
Dim strQ As String = "SELECT MAX(IDbatch) from batches "
Dim IDbatch As Integer
Dim cmdQ As OleDbCommand = New OleDbCommand(strQ, con)
If Not con.State = ConnectionState.Open Then
con.Open()
End If
Dim QReader As OleDbDataReader
Dim it As Integer
QReader = cmdQ.ExecuteReader
If QReader.FieldCount > 0 Then
While QReader.Read
it = QReader.Item("IDbatch")
MsgBox(it)
End While
End If
I am getting Out of range error
Change your query to
Dim strQ As String = "SELECT MAX(IDbatch) as MaxIDbatch from batches "
and your code that read the value to
it = QReader.Item("MaxIDbatch")
As you have it now the MAX(IDbatch) creates a field with a different name than IDbatch and trying to retrieve the content of that field using the name IDbatch fails giving the Index out of range exception
By the way, your check for FieldCount > 0 will be always true also in cases where there is no record in the batches table. So, if you have done that to protect in case of no record, then it is better to write
Dim result = cmdQ.ExecuteScalar()
if result IsNot Nothing then
Dim it = Convert.ToInt32(result)
MsgBox(it)
End If
And with this approach you could also leave out the aliasing on the IDbatch field

"No data exists for the row/column" when connecting to SQL database from VB.net

I'm trying to create a program which has a datagridview, when the user clicks on a cell in the view, it then looks in a SQL database, grabs information from other fields in the same record, and automatically fills corresponding text boxes (done by manipulating the name of the field) in the form.
For some reason however, I'm getting an error message saying:
"InvalidOperationException was unhandled"
"No Data exists for the row / column"
Here is the code relevant to this part of the program:
Private Sub DataGridView1_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvResults.CellMouseClick
' Set values in the text boxes to both corresponding to the film.
Dim strFields() As String = {"ID", "fName", "fGenre", "fSynopsis", "fAgeRating", "fActorsActresses", "fWebRating", "fNoReservations", "fWeeksRunning"}
Dim Con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=ApplicationData.accdb;Persist Security Info=False;")
Con.Open() 'Open the connection
Dim Cmd As New OleDbCommand(StringBuilderCommand("*", "Films", dgvResults.CurrentCell.Value, "fName"), Con) 'Create a string by calling the StringBuilderCommand to combine the parameters together with quotes.
Cmd.CommandType = CommandType.Text
Dim Rdr As OleDbDataReader = Cmd.ExecuteReader()
Dim intCount As Integer = 4 ' Create a loop variable.
Do While Rdr.Read() Or intCount < 6 ' While this statement is 'TRUE', e.g. there is a valid record.
strResult = "txt" & strFields(intCount).Replace("f", "") 'Remove any instances of 'f', e.g. the prefix of the string.
txtActorsActresses.Text = StringBuilderCommand("*", "Films", dgvResults.CurrentCell.Value, "fName")
Me.Controls(strResult).Text = Rdr.Item(strFields(intCount)) ' Suspect the error lies here.
'Set the text-box to the correct value from the database.
'This will allow me to go through several text boxes, and grab their corresponding values from the database.
intCount = intCount + 1
'Current error is because it cannot find any data beyond the first field taken.
'I have no idea why this is. But if I change the starting intCount value, it will successfully take a different value.
Loop
Rdr.Close() 'Cleaning up.
Cmd.Dispose()
Con.Close()
WebBrowser1.Navigate(dgvResults.CurrentCell.Value.Replace(" ", ".") & ".movie.poster.new.jpg.to") 'Grab the movie poster off the internet corresponding to the films name.
End Sub
Private Function StringBuilderCommand(Field, Table, CurrentCellValue, SearchParameter)
'Creates a suitable SQL string.
Dim MyStringBuilder As New StringBuilder("SELECT ")
MyStringBuilder.Append("*") ' Append the parameter 'Field'.
MyStringBuilder.Append(" FROM ") ' Append the SQL command 'FROM'.
MyStringBuilder.Append(Table) ' Append the parameter 'Table'.
MyStringBuilder.Append(" WHERE ") ' Append the SQL command 'WHERE'.
MyStringBuilder.Append(SearchParameter) ' Append the parameter 'SearchParameter'.
MyStringBuilder.Append("=""")
MyStringBuilder.Append(CurrentCellValue) ' Append the parameter 'CurrentCellValue', representing the cell selected.
MyStringBuilder.Append("""") 'Append a quotation mark.
Return MyStringBuilder.ToString() ' Return it to the main program.
End Function
Database table being connected to:
A view of the error as it looks in Visual Studio 2012 Express:
The value of 'dgvResults.CurrentCell.Value' is the name of a film taken from the database (e.g. "12 Years a Slave").
What am I doing wrong?
Thanks,
C.
The problem is caused by the value of strFields(intCount) you are passing to the reader. It is not a valid column index.
You probably want to loop on the fields before looping again on DataReader(), like:
Do While Rdr.Read()
For intCount as Integer = 4 to 6
strResult = "txt" & strFields(intCount).Replace("f", "")
txtActorsActresses.Text = StringBuilderCommand("*", "Films", dgvResults.CurrentCell.Value, "fName")
Me.Controls(strResult).Text = Rdr.Item(strFields(intCount))
Next
Loop
I removed the Dim intCount As Integer = 4 because it is no longer needed because of the for next loop.

Email address getting in encoded format DB

I'm getting the email address in encoded format like "annie#h ꇟ|(ƓƓⲘ" and i'm catching it in a string then not able to store it in server database. So how do i decode it to normal email address. or not getting which type of Encoding is it(base64/ascii/ect..).and the column type is long varchar,
machine i'm using it windows xp. I'm pulling my hair out.
Please help..
i caught the answer but i'm not sure is this the right way to do.
Now I read the record from ADODB.Recordset instead of Dataset.
is this the right way to read data.
I don't know why Dataset give me the wired email address,But using Record set i solve the issue
Here is the code sample that i use now
Dim str_query = "select * from table"
Dim objRS= New ADODB.Recordset
objRS= Cn.Execute(str_query )
Do While Not objRS.EOF
For k = 0 To objRS.Fields.Count - 1
Debug.Print objRS(k).Name & ": " & objRS(k).Value
Next
Debug.Print "_____"
objRS.MoveNext
Loop
And previously I used code this
Dim str_query = "select * from table"
Dim objRS= New ADODB.Recordset
objRS= Cn.Execute(str_query )
Dim ds As DataSet = New DataSet()
Dim da As OleDbDataAdapter = New OleDbDataAdapter
da.Fill(ds, objRS, "my_table")
For Each dr As DataRow In ds.Tables("my_table").Rows
Next

Trying to make MS SQL store CRLF

How can I get SQL 2008 R2 to store the CR/LF embedded in a string (so 2 paragraphs, or possibly multiple CR/LFs) into a field of a record being inserted programmatically vb.net? Not all records have a CR/LF so I cannot just add them to every record. +Char(13)+char(10) that I could split with a charArrary.
UPDATE: This appears to be the actual problem: the XML parser will convert CR/LF to LF only for consistency. I am passing data to and from a web service and this seems to be where the CR is being lost. This is by W3 standard.
W3.org Section: 2.11 End-of-Line Handling
These strings are coming from an Access DB and have the CR/LF already included in a multi-paragraph field of the record. I would like to just pass them straight in, but SQL is converting them to spaces.
In VS Sql Editor the record appears to store 20:20 Hex where the CR/LF should reside. The string actually should have Char(13) & Char(10) or 0x0D0A but it does not.
I see the same results even editing or inserting the record directly via SSMS or the VS SQL Editor. The records were originally submitted via a web service but I have eliminated that for testing. I read that the XML parser will also convert CR/LF to LF only for consistency.
I am not able to show the actual query. But the adapter is built here and populated and passed back to my app via a web service call. This process works for all but the records that have CRLF in them.
Dim strConnection As String
strConnection = ConfigurationManager.ConnectionStrings("ConnectionStringMyDB").ConnectionString
Dim myConnection As New SqlConnection(strConnection)
Dim myCommand As New SqlCommand(strSQL, myConnection)
' Access file name
myCommand.Parameters.AddWithValue("#dbName", OleDbType.VarChar).Value = dbName
' Table in Access DB
myCommand.Parameters.AddWithValue("#tableName", OleDbType.VarChar).Value = tableName
myConnection.Open()
' Create the DataAdapter
Dim myDataAdapter As New SqlDataAdapter()
myDataAdapter.SelectCommand = myCommand
' Populate the DataSet and close the connection
Dim myDataSet As New DataSet()
myDataAdapter.Fill(myDataSet)
myConnection.Close()
Return myDataSet
Processsing in my app after assigning the dataset to a table.
For Each row As DataRow In myTable.Rows
Dim stringVal As String = row(columnName).ToString()
' Eventually added this to see that the row was adding 2 spaces after the carriage return
Dim cstringVal() As Char = stringVal.ToCharArray
Dim csearchValue() As Char = searchValue.ToCharArray
' Also added this code
Dim line_array1 As [String]() = stringVal.Split(vbCr & vbLf.ToCharArray())
Dim line_array2 As [String]() = searchValue.Split(vbCr & vbLf.ToCharArray())
' line_Array2 consistently IDs the vbCRLF, line_array1 never does.
' Originally tried
If row(columnName) = searchValue Then
return True
End If
' Tried this
If stringVal = searchValue Then
Return True
End If
' And this
If String.Compare(stringVal, searchValue, False) = 0 Then
Return True
End If
Next
Return False
Thanks
Maybe the problem is UNICODE. By my experience there is no problem with storing CRLF, but there can be problem UNICODE <-> ASCII.
Please try
Dim cmd as SQLCommand
cmd.CommandText = "insert into MyTable(fld) values (N'" + VariableContainingCRLF +"')"
cmd.ExecuteNonQuery
Or
Dim cmd as SQLCommand
cmd.CommandText = "insert into MyTable(fld) values (#param)"
cmd.Parameters.Add("#param", SqlDbType.NVarChar, 50).Value = VariableContainingCRLF
cmd.ExecuteNonQuery
Letter N before a string or NVarChar is crucial. DataAdapter introduces just other confusing layer.
You are confusing data storage and data display and editing.
I just verified that SQL Server does store CRLF in a VARCHAR field:
insert into MyTable (VarcharField)
values ('A'+CHAR(13)+'B')
, ('A'+CHAR(13)+CHAR(10)+'B')
, ('A
B')
by checking the ASCII() codes of the inserted strings
SELECT VarcharField, LEN(VarcharField),
, ASCII(substring(VarcharField, 2, 1))
, ASCII(substring(VarcharField, 3, 1))
FROM MyTable
The CR and CRLF are displayed as spaces, but they are still stored as 0x0D and 0x0A, or 13 and 10.