System.ArgumentException: 'Cannot set column 'ort'. The value violates the MaxLength

  • Thread starter Thread starter Rocky48
  • Start date Start date
R

Rocky48

Guest
I have an exception that dosen't make any sense to me.
I have a column in my database that has 'P' for Portrait and 'L' for Landscape, which in the database is a varchar(1).
Here is the full output message:

System.ArgumentException: 'Cannot set column 'ort'. The value violates the MaxLength limit of this column.'
Display
System.ArgumentException
HResult=0x80070057
Message=Cannot set column 'ort'. The value violates the MaxLength limit of this column.
Source=System.Data
StackTrace:
at System.Data.DataColumn.CheckMaxLength(DataRow dr)
at System.Data.DataTable.RaiseRowChanging(DataRowChangeEventArgs args, DataRow eRow, DataRowAction eAction, Boolean fireEvent)
at System.Data.DataTable.SetNewRecordWorker(DataRow row, Int32 proposedRecord, DataRowAction action, Boolean isInMerge, Boolean suppressEnsurePropertyChanged, Int32 position, Boolean fireEvent, Exception& deferredException)
at System.Data.DataRow.EndEdit()
at System.Data.DataRowView.EndEdit()
at System.Windows.Forms.CurrencyManager.EndCurrentEdit()
at System.Windows.Forms.CurrencyManager.ChangeRecordState(Int32 newPosition, Boolean validating, Boolean endCurrentEdit, Boolean firePositionChange, Boolean pullData)
at System.Windows.Forms.CurrencyManager.set_Position(Int32 value)
at System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e)
at System.Windows.Forms.ComboBox.WmReflectCommand(Message& m)
at System.Windows.Forms.ComboBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ComboBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at My_Greeting.My.MyApplication.Main(String[] Args) in :line 81

Why does it say that it excedes the MaxLength limit of this column, as the column is one character wide and only has either the character 'P' or 'L' in the column.
I don't know if this a clue, but it works upto CID = 9? (Where CID is the ID column and is an int.)

'ort' was my variable name that represented Orientation. As I stated before in the database it is varchar(1). this column has either P or L in the database.
I still find it strange that it does not get an exception error until the 10th row and all rows after that?

As the error to some extent concerns the data table here is a screen shot of the settings:

1460296.png

here is the code for the form:

Imports PdfSharp
Imports PdfSharp.Drawing
Imports PdfSharp.Fonts
Imports PdfSharp.PageOrientation
Imports PdfSharp.PageSize
Imports PdfSharp.Pdf
Imports PdfSharp.Internal
Imports PdfSharp.Drawing.Layout
Imports PdfSharp.Forms
Imports System.Data.SqlClient

Enum pageOrientation
Landscape
Portrait
End Enum
Enum pagesize
A4
A5
End Enum

Public Class frmPrintFrm

Private boxX As Double
Private boxY As Double
Private cellw As Double
Private cellh As Double
Private ort As String = Nothing
Private FSize As Integer
Private CFont As String = Nothing
Private TxtColor As String
Private sqlAdaptor As SqlDataAdapter
Private dt As New DataTable
Private CSizeSql As String = Nothing
Private cmd As Object = Nothing
Private CSizeValue As Double
Private Narrative As String = Nothing
Private CID As Integer
Private Y As Double
Private Verse As String
Private connectionString As String = "Data Source=DESKTOP-S7FRNAL\SQLEXPRESS;Initial Catalog=Verses_Find;Integrated Security=True"
Private Sub myBase_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.CSizeTableAdapter.Fill(Me.Verses_FindDataSet5.CSize)
Dim Print As New frmPrintFrm
Me.TopMost = True
Me.WindowState = FormWindowState.Normal

For Each oFont As FontFamily In FontFamily.Families 'This line populates the font combo with the system installed fonts
cboFont.Items.Add(oFont.Name)
Next
'fetch the XKnownColor values into cboColor
Dim knownColours() As XKnownColor = XColorResourceManager.GetKnownColors(includeTransparent:=False)
cboColor.DataSource = knownColours
cboColor.SelectedIndex = 0
End Sub
Private Sub btnPaste_Click(sender As Object, e As EventArgs) Handles btnPaste.Click

' Determine if there is any text in the Clipboard to paste into the text box.
If Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) = True Then
' Determine if any text is selected in the text box.
If txbVerse.SelectionLength > 0 Then
' Ask user if they want to paste over currently selected text.
If MessageBox.Show("Do you want to paste over current selection?",
"Cut Example", MessageBoxButtons.YesNo) = DialogResult.No Then
' Move selection to the point after the current selection and paste.
txbVerse.SelectionStart = txbVerse.SelectionStart + txbVerse.SelectionLength
End If
End If
' Paste current text in Clipboard into text box.
txbVerse.Paste()
End If
End Sub
Private Sub cboCSize_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboCSize.SelectedIndexChanged
CSizeValue = CInt(cboCSize.SelectedValue)
End Sub
Private Sub nudFSize_ValueChanged(sender As Object, e As EventArgs) Handles nudFSize.ValueChanged
FSize = CInt(nudFSize.Value)
End Sub

Private Sub cboFont_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboFont.SelectedIndexChanged
CFont = cboFont.Text
End Sub

Private Sub nudTop_ValueChanged_1(sender As Object, e As EventArgs) Handles nudTop.ValueChanged
boxY = CInt(nudTop.Value)
End Sub
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click

Using connection As New SqlConnection(connectionString), da As New SqlDataAdapter("SELECT CID, BoxX, Cellw, Cellh, ort, Narrative FROM CSize WHERE CID = @CID", connection), dt As New DataTable
connection.Open()

da.SelectCommand.Parameters.AddWithValue("@CID", CSizeValue)
da.Fill(dt)
If dt.Rows.Count > 0 Then
Dim row As DataRow = dt.Rows(0)
printIt(row)



End If

End Using


End Sub
Private Sub printIt(row As DataRow)
Dim ID As Double = CDbl(row("CID"))
Dim X As Double = CDbl(row("BoxX")) * 2.83465
Dim W As Double = CDbl(row("Cellw")) * 2.83465
Dim H As Double = CDbl(row("Cellh")) * 2.83465
Dim O As String = CStr(row("ort"))
Dim N As String = CStr(row("Narrative"))
Dim Y As Double
Dim NL As String = CStr(Chr(13) & Chr(10))
Verse = txbVerse.Text
Replace(Verse, NL, " VBCrLf ")
Dim document As PdfDocument
' Create a new PDF document
document = New PdfDocument()
document.Info.Title = "Created with PDFsharp"

' Create an empty page
Dim page As PdfPage = document.AddPage

If O = "L" Then
page.Orientation = CType(pageOrientation.Landscape, PdfSharp.PageOrientation)
page.Width = XUnit.FromMillimeter(297)
page.Height = XUnit.FromMillimeter(210)
Else
page.Orientation = CType(pageOrientation.Portrait, PdfSharp.PageOrientation)
page.Width = XUnit.FromMillimeter(210)
page.Height = XUnit.FromMillimeter(297)
End If

' create the brush
Dim xClr As XColor = XColor.FromKnownColor(CType(cboColor.SelectedItem, XKnownColor))
Dim brush As XSolidBrush = New XSolidBrush(xClr)

' Draw the text
Y = boxY * 2.834665

Dim gfx As XGraphics
gfx = XGraphics.FromPdfPage(page)
Dim tf As XTextFormatter
tf = New XTextFormatter(gfx)
Dim font As XFont = New XFont(CFont, FSize, XFontStyle.Regular)
Dim rect As XRect
rect = New XRect(X, Y, W, H)
gfx.DrawRectangle(XBrushes.SeaShell, rect)
tf.Alignment = XParagraphAlignment.Center
tf.DrawString(Verse, font, brush, rect, XStringFormat.TopLeft)

' Save the document
Dim filename As String = "verse.pdf"
document.Save(filename)

' ...and start a viewer.
Process.Start(filename)
Me.Close()
End Sub
End Class

'ort' was my variable name that represented Orientation. In the database it is varchar(1) and this column has either P or L in the database.
I find it strange that it does not get an exception error until the 10th row and all rows after that?

I have tried debugging and I found the following:

The exception appears after the CSize sub is exited, but only in the case that the ID field (CID) reaches 10.

I tried selecting one of the rows where CID was less than 10 and the combobox did not show the selected text util it exited the sub. The combo was set using the VS combobox control.

1460298.png

As you will see that I use 'ort ' as the selected value, beacause the code requires this to determine the orientation of the paper. Whilst writing this I just realised that I had the conversion set to 'int' whereas the selected value is a string character. I think I should be using the 'Value Member' (CID), but the normal type of the 'Value Member' is a string and I have tried to convert it to an integer by using Cint and also CType, but it says you can't do that.

I am sure the problem lies in the fact that I want the value of 'ort' (string), but also I need the CID value (int) in the SELECT statement which sets all of the positioning dimensions of the text.

However I can't see why it calls the exception on the Selected Value (ort)?

Can anyone see where I am going wrong?







TEH

Continue reading...
 
Back
Top