Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm trying to insert some records in a MySQL database with a DataSet but I'm getting an error when I try to set the value of a column. This is the code I'm using:

 

Dim odbcDataSet As New DataSet
Dim odbcDataAdapter As New OdbcDataAdapter(New OdbcCommand("SELECT * FROM MyTable", dbMySql))

odbcDataAdapter.InsertCommand = New OdbcCommand("INSERT INTO MyTable (MyColumn) VALUES (@MyColumn)", dbMySql)

odbcDataAdapter.InsertCommand.Parameters.Add(New OdbcParameter("@MyColumn", OdbcType.VarChar, 30, "MyColumn"))

odbcDataAdapter.FillSchema(odbcDataSet, SchemaType.Source)

odbcDataSet.Tables(0).TableName = "MyTable"

Dim odbcDataRow As DataRow = odbcDataSet.Tables("MyTable").NewRow()

odbcDataRow("MyColumn") = "yhjhgj"

odbcDataSet.Tables("MyTable").Rows.Add(odbcDataRow)

odbcDataAdapter.Update(odbcDataSet, "MyTable")

 

The MySql server is version 3.something and this is the error I'm getting:

 

System.ArgumentException: Cannot set column 'MyColumn' to 'yhjhgj'. The value violates the MaxLength limit of this column.

 

Why is this error occuring while I set the columns length to 30 (which is VARCHAR(30))?

  • *Experts*
Posted

To add to sam's comment, you have the following line which will get you the maxlength of the column in the DB:

odbcDataAdapter.FillSchema(odbcDataSet, SchemaType.Source)

 

You should be able to check the DB's length with:

Dim i As Integer = odbcDataSet.Tables(0).Columns("MyColumn").MaxLength

 

-nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

I've checked the column again samsmithnz but I'm sure it's VARCHAR(30) :p

 

And when I get the column size it's MaxLength is 8? So ADO.NET doesn't seem te be able to get the correct column length...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...