Hello,

I am having a problem with adding a row to my DataSet. I think I know what the problem is, but not how to correct it. I have several bound text boxes that fill from the DataSet. The first column is a numeric and the next several are text strings. When I try to add the new row it hangs at the first entry and gives me a wrong type error. I think the problem is that when it tries to update from the text box with new information, it is trying to enter Text into a Numeric field. My question is how do I convert the data to the proper type during the update? I am including part of the code here for claification. The database is a one table Access database. VID is the Primary Key in the table.

Code:

Private Sub btnAdd_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

Dim drNew As System.Data.DataRow

drNew = Me.DsHeartland1.Customer.NewRow
drNew.Item("VID") = ("New VID")
drNew.Item("Customer Name") = ("New Customer Name")
drNew.Item("Address") = ("New Address")
drNew.Item("City") = ("New City")
drNew.Item("State") = ("New State")
drNew.Item("Zip") = ("New Zip")
drNew.Item("Phone 1") = ("New Phone 1")
drNew.Item("Phone 2") = ("New Phone 2")
drNew.Item("Phone 3") = ("New Phone 3")
drNew.Item("Fax") = ("New Fax")
drNew.Item("Email") = "New Email"
Me.DsHeartland1.Customer.Rows.Add(drNew)

The debugger will stop and error at the line adding "VID" which is a numeric entry.

Any help will be greatly appreciated!

Dani AI

Generated

Brief diagnosis and targeted fixes for 's problem: the error happens because a string is being assigned to a numeric DataColumn (VID). was on the right track suggesting numeric conversion, but the conversion must be performed on the actual input and guarded against empty/invalid text. If VID is an Access AutoNumber (generated by the database), it should normally be left unset on the new DataRow and allowed to be supplied by the database when the row is inserted.

Practical steps (apply in code before calling Rows.Add or when preparing values):

  • Confirm the column type at runtime (helps verify whether VID is Int32):

    Debug.WriteLine(DsHeartland1.Tables("Customer").Columns("VID").DataType.FullName)
  • Convert safely from a textbox or string source:

    Dim vid As Integer
    If Integer.TryParse(txtVID.Text, vid) Then
      drNew("VID") = vid
    Else
      drNew("VID") = DBNull.Value    ' or handle as validation failure
    End If
  • If VID is an AutoNumber in Access, do not try to set a human-friendly string. Either omit assigning VID or make the DataTable generate a temporary key so client-side code can work with the new row before update:

    Dim col = DsHeartland1.Tables("Customer").Columns("VID")
    col.AutoIncrement = True
    col.AutoIncrementSeed = -1
    col.AutoIncrementStep = -1

Additional notes: bound TextBox controls that are bound to numeric columns will attempt to push their Text (a string) into the DataRow and will throw a type mismatch unless formatting/parse handling is provided or the input is validated first. Use Integer.TryParse (preferable to Convert.ToInt32 on raw input) and inspect the debugger/Exception Assistant for the precise exception (often an InvalidCastException for type mismatches). 's recommendation to consult VB.NET database programming resources can help for end-to-end adapter/update patterns, but the immediate fix is to correct the type handling or let the DB supply the VID.

Recommended Answers

All 3 Replies

Try this,
Convert.ToInt32("" + VariableName + "")

Thanks for the suggestion. When I tried it I received an error saying that VID was not defined. I tried it without the extra quotes and + and had an error saying "This code has called into another function. When that function is finished the next statement will be executed". I am using .Net 2002, don't know if that would make a difference or not. It seems like it should be so simple and I am sure it is, just can't see it yet! :confused:

Hi,

This book " Data base programming using vb.net and sqlserver" will help you to develop the full application in .Net.

try this link:

regards
bhar

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.