Problem updating database with da.Update

dazlerd

Well-known member
Joined
Sep 29, 2006
Messages
52
Programming Experience
5-10
Hi

I have an XML file which contains 30 new "customers" that need adding to the database. Each <customer> record has a customerid, email and password. These fields get added into the datatable correctly.

I've tried the following but I get an error "column 'email' cannot be null" when calling da.Update(ds, "Customer"). I am using vb.net 2005 with a MySQL db.

VB.NET:
     Private Sub InsertIntoCustomersTable(ByVal XMLString As String)
        Dim ds As New DataSet
        Dim da As New MySqlDataAdapter
        Dim sr As New System.IO.StringReader(XMLString)

        ' Set the data adapter statements
        getCustomerDataset(ds, da)
        ' Read in the XMLString to populate the table
        ds.ReadXml(sr, XmlReadMode.IgnoreSchema)
        ' Can we simply update db
        da.Update(ds, "Customer")

        ds.Dispose()
    End Sub

    Public Sub getCustomerDataset(ByRef result As DataSet, ByRef adapterCustomer As MySqlDataAdapter)

        Dim sql As String

        sql = "SELECT CustomerId, Email, password FROM customer WHERE 1 = 1"
        adapterCustomer = New MySqlDataAdapter(sql, CAS.DataConnection.getLocalConnection)
        If result.Tables.Contains("Customer") Then result.Tables("Customer").Clear()
        adapterCustomer.Fill(result, "Customer")

        sql = "INSERT INTO customer (Email, password) VALUES (@Email, @password); "
        sql = sql & "SELECT CustomerId, email, password FROM customer WHERE CustomerId = SCOPE_IDENTITY()"
        Dim iCmd As MySqlCommand = New MySqlCommand(sql, CAS.DataConnection.getLocalConnection)
        iCmd.Parameters.Add(New MySqlParameter("@EMAIL", MySqlDbType.VarChar, 50, "Email"))
        iCmd.Parameters.Add(New MySqlParameter("@password", MySqlDbType.VarChar, 100, "password"))
        adapterCustomer.InsertCommand = iCmd

        sql = "UPDATE customer SET [CustomerId] = @CustomerId, [email] = @Email, [password] = @password "
        sql = sql & "WHERE [CustomerId] = @CustomerId; "
        sql = sql & "SELECT CustomerId, email, password FROM customer WHERE CustomerId = SCOPE_IDENTITY()"
        Dim uCmd As MySqlCommand = New MySqlCommand(sql, CAS.DataConnection.getLocalConnection)
        uCmd.Parameters.Add(New MySqlParameter("@CustomerId", MySqlDbType.Int32, 0, "Customer_id"))
        uCmd.Parameters.Add(New MySqlParameter("@Email", MySqlDbType.VarChar, 50, "Email"))
        uCmd.Parameters.Add(New MySqlParameter("@password", MySqlDbType.VarChar, 100, "password"))
        adapterCustomer.UpdateCommand = uCmd
    End Sub

What could be wrong?

Thanks

Darren
 
Last edited:
It actually sounds like MySQL is a bunch of ass when it comes to using with VS.NET; i would find another rdbms!
 
so are SQLS Express and Oracel10g Express.. and say you get paid £100 a day, and using a "free" product hinders your development such that you spend 3 days buggering about getting the DAL working and a further 2 days because you have to write more code with your fingers rather than dragdrop.. was it really free?
I'd have bought a licence for SQLS with that £500, heck.. pass the cost onto the customer; it's still free. Tell them to run a pirate version if they want; its not your knackers on the chopping block when FACT come bursting through the door :D


Pragma!
 
Hi CJard

Ive created a test table inside MSSQL with the same structure as the promblematic one in MySQL. I have also created a typed dataset from within VS.NET.

My XML with one record looks like:

HTML:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<Customers>
<Customer>
<customer_id>2931</customer_id>
<email>darren.rhymer@mouselink.co.uk</email>
<password>ab8a898d78af351c0816282a0c34d53c</password>
</Customer>
</Customers>

The code I am running now looks like:
VB.NET:
[SIZE=1]    Dim ds As New DataSet                   ' Create a data set to hold the XML data[/SIZE]
[SIZE=1]    Dim da As New MySqlDataAdapter[/SIZE]
[SIZE=1]    Dim sr As New System.IO.StringReader(XMLString)  ' Create a StringReader to store the XML data[/SIZE]
 
[SIZE=1]    ' Set the data adapter statements[/SIZE]
[SIZE=1]    getCustomerDataset(ds, da)[/SIZE]
 
[SIZE=1]    Me.customerTableAdapter.Fill(Mssqlds.customer)[/SIZE]
 
[SIZE=1][COLOR=red]' This line reads it into the ds dataset correctly.[/COLOR][/SIZE]
[SIZE=1]    ds.ReadXml(sr, XmlReadMode.IgnoreSchema)[/SIZE]
 
[SIZE=1]    ' Read in the XMLString to populate the table[/SIZE]
[SIZE=1][COLOR=red]' This line errors (see below)[/COLOR][/SIZE]
[SIZE=1]    Mssqlds.customer.ReadXml(XMLString)[/SIZE]
 
[SIZE=1]    ' Can we simply update db[/SIZE]
[SIZE=1]    Me.customerTableAdapter.Update(Mssqlds.customer)[/SIZE]
 
[SIZE=1]    Mssqlds.customer.Dispose()[/SIZE]

The specified line above errors with the message "Illegal characters in path."

How can it work using untyped ds but wont let me enter it into a typed dataset?

Thanks

Darren
 
Last edited by a moderator:
I'm lost.. that XML you posted looks nothing like what I'd expect a typed DataSet's XML to be..

VB.NET:
<?xml version="1.0" standalone="yes"?>
<DataSet1 xmlns="[URL]http://tempuri.org/DataSet1.xsd[/URL]">
  <APPS_PREFS>
    <PREF_APP>aparan</PREF_APP>
  </APPS_PREFS>
  <APPS_PREFS>
    <PREF_APP>bossman</PREF_APP>
  </APPS_PREFS>
  <APPS_PREFS>
    <PREF_APP>boston</PREF_APP>
  </APPS_PREFS>
  <APPS_PREFS>
    <PREF_APP>credaro</PREF_APP>
  </APPS_PREFS>
  <APPS_PREFS>
    <PREF_APP>ddhub</PREF_APP>
  </APPS_PREFS>
  <APPS_PREFS>
    <PREF_APP>miradmin</PREF_APP>
  </APPS_PREFS>
  <APPS_PREFS>
    <PREF_APP>probrep</PREF_APP>
  </APPS_PREFS>
  <APPS_PREFS>
    <PREF_APP>startbay</PREF_APP>
  </APPS_PREFS>
  <APPS_PREFS>
    <PREF_APP>tray</PREF_APP>
  </APPS_PREFS>
</DataSet1>

That's the xml from a typed dataset. The named XSD is:

VB.NET:
<xs:schema id="DataSet1" targetNamespace="http://tempuri.org/DataSet1.xsd" xmlns:mstns="http://tempuri.org/DataSet1.xsd" xmlns="http://tempuri.org/DataSet1.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop" attributeFormDefault="qualified" elementFormDefault="qualified">
  <xs:annotation>
    <xs:appinfo source="urn:schemas-microsoft-com:xml-msdatasource">
      <DataSource DefaultConnectionIndex="0" FunctionsComponentName="QueriesTableAdapter" Modifier="AutoLayout, AnsiClass, Class, Public" SchemaSerializationMode="IncludeSchema" xmlns="urn:schemas-microsoft-com:xml-msdatasource">
        <Connections>
          <Connection AppSettingsObjectName="MySettings" AppSettingsPropertyName="ConnectionString" ConnectionStringObject="" IsAppSettingsProperty="True" Modifier="Assembly" Name="ConnectionString (MySettings)" PropertyReference="ApplicationSettings.WindowsApplication2.My.MySettings.GlobalReference.Default.ConnectionString" Provider="System.Data.OracleClient">
          </Connection>
        </Connections>
        <Tables>
          <TableAdapter BaseClass="System.ComponentModel.Component" DataAccessorModifier="AutoLayout, AnsiClass, Class, Public" DataAccessorName="APPS_PREFSTableAdapter" GeneratorDataComponentClassName="APPS_PREFSTableAdapter" Name="APPS_PREFS" UserDataComponentName="APPS_PREFSTableAdapter">
            <MainSource>
              <DbSource ConnectionRef="ConnectionString (MySettings)" DbObjectName="GTP.APPS_PREFS" DbObjectType="Table" FillMethodModifier="Public" FillMethodName="Fill" GenerateMethods="Both" GenerateShortCommands="False" GeneratorGetMethodName="GetData" GeneratorSourceName="Fill" GetMethodModifier="Public" GetMethodName="GetData" QueryType="Rowset" ScalarCallRetval="System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" UseOptimisticConcurrency="True" UserGetMethodName="GetData" UserSourceName="Fill">
                <SelectCommand>
                  <DbCommand CommandType="Text" ModifiedByUser="True">
                    <CommandText>select distinct pref_app from apps_prefs</CommandText>
                    <Parameters>
                    </Parameters>
                  </DbCommand>
                </SelectCommand>
              </DbSource>
            </MainSource>
            <Mappings>
              <Mapping SourceColumn="PREF_APP" DataSetColumn="PREF_APP" />
            </Mappings>
            <Sources>
            </Sources>
          </TableAdapter>
        </Tables>
        <Sources>
        </Sources>
      </DataSource>
    </xs:appinfo>
  </xs:annotation>
  <xs:element name="DataSet1" msdata:IsDataSet="true" msdata:UseCurrentLocale="true" msprop:Generator_UserDSName="DataSet1" msprop:Generator_DataSetName="DataSet1">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="APPS_PREFS" msprop:Generator_UserTableName="APPS_PREFS" msprop:Generator_RowDeletedName="APPS_PREFSRowDeleted" msprop:Generator_TableClassName="APPS_PREFSDataTable" msprop:Generator_RowChangedName="APPS_PREFSRowChanged" msprop:Generator_RowClassName="APPS_PREFSRow" msprop:Generator_RowChangingName="APPS_PREFSRowChanging" msprop:Generator_RowEvArgName="APPS_PREFSRowChangeEvent" msprop:Generator_RowEvHandlerName="APPS_PREFSRowChangeEventHandler" msprop:Generator_TablePropName="APPS_PREFS" msprop:Generator_TableVarName="tableAPPS_PREFS" msprop:Generator_RowDeletingName="APPS_PREFSRowDeleting">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="PREF_APP" msprop:Generator_UserColumnName="PREF_APP" msprop:Generator_ColumnPropNameInRow="PREF_APP" msprop:Generator_ColumnVarNameInTable="columnPREF_APP" msprop:Generator_ColumnPropNameInTable="PREF_APPColumn">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:maxLength value="10" />
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>
 
Cjard

Sorry that XML is the XML im trying to enter into the typed dataset. At this stage that is to a MSSQL database.

Ive been following the instruction from the MySQL post and ive hit a problem here. I cant start MySQL with the sql-mode=ANSI_QUOTES. This means I cant drag and drop a MySQL table frm the server explorer to the dataset.

So for me this project is going really well !! lol. I cant save the xml using a typed dataset to a MSSQL db, and I cant use MySQL as the database for the typed datasets !!! Happy Days !

Thanks
 
Incidentally, I had issues trying to import xml data into a typed dataset if I removed the xmlns="http://tempuri.org/DataSet1.xsd"

It imported nothing, even though it had just exported the data itself..

This is, after all, logical;a typed dataset needs a schema to describe the types therewithin
 
Yeah Ive been thinking about this, heres my new xml to import:

VB.NET:
<?xml version="1.0" encoding="UTF-8"?>
<Customers xmlns="http://tempuri.org/Mssqlds.xsd">
<Customer>
<customer_id>2931</customer_id>
<email>dazlerd@domain.co.uk</email>
<password>ab8a545d78af351c1245882a0c34d53c</password>
</Customer>
</Customers>


My code looks like :
VB.NET:
Private Sub InsertIntoCustomersTable(ByVal XMLString As String)
  Dim sr As New System.IO.StringReader(XMLString)
  Mssqlds.Customer.ReadXml(sr)
  Me.CustomerTableAdapter.Update(Mssqlds.Customer)
End Sub

This adds the columns to the datatable but the row.count = 0.
 
Last edited:
The row.count of the datatable Mssqlds.Customer.

The line

Mssqlds.Customer.ReadXml(sr)

adds the columns to the datatable but no actual records are added.
 
Im puzzled; this is a typed dataset; the columns should already be there.. ReadXml shouldnt add them..

Can you send me:

The xml file
Any XSD for any datasets you are using..
 
Nnnnnyyyyyyyyyoooooooooooouuuuuuuuuuuuuu spanner... :D

It might help if the XML youre wanting to import into a table called CCCustomer in a dataset called mssqlds actually had those names in the XML:

VB.NET:
<?xml version="1.0" encoding="UTF-8"?>
<[B][COLOR=red]Customers[/COLOR][/B] xmlns="[URL]http://tempuri.org/Mssqlds.xsd[/URL]">
  <[B][COLOR=red]Customer[/COLOR][/B]>
    <customer_id>2931</customer_id>

i.e. :
VB.NET:
<?xml version="1.0" encoding="UTF-8"?>
<[B][COLOR=seagreen]mssqlds [/COLOR][/B]xmlns="[URL]http://tempuri.org/Mssqlds.xsd[/URL]">
  <[B][COLOR=seagreen]CCCustomer[/COLOR][/B]>
    <customer_id>2931</customer_id>


ReadXml(XMLString) fails because the only string overload for ReadXml requires a path to an xml file, not a string full of xml..

i.e. ReadXml("C:\temp.xml") not ReadXml("<xml><thing>a</thing></xml>")
 
Last edited:
oh my god what an arse !! lol. Sorry

Yeah when I removed the dataset and started again I called the table simple Customer !! Thanks for letting me know why it worked !

But i did get it to work without replacing Customers with mssqlds. Is that right?

Thanks

Darren

p.s Ive just imported 2928 records from my remote db into my local db. Now how do we change it from a MSSQL db to a MySQL db. :D
 
I'm guessing the dataset name is less important in this case because regardless of its name, it obeys the XSD laid out in the url hence can be imported into. Table name is very important because its the only way of finding it in a collection.

If you leave mssqlds as Customer, then Me.Mysqlds.Name might return "Customer" - i dont know.. either way, its unlikely to be a breaking change because the dataset is the top level container..


Take all the code the designer generated for you and retrofit it with MySQL stuff.. Find replace might get you far enough..
 
Hi

Ive just spent ages changing things like

SqlClient.SqlCommand to

MySql.Data.MySqlClient.MySqlCommand etc

when there were no errors left i saved all files.

I changed all instances of boaConnectionString (the one VS created when i dragged a MSSQL table from the server explorer onto the dataset) to mainConnectionString (my MySQL connection).

But I cant preview data from the datatables. I think its to do with:

<DbSource ConnectionRef="mainconnectionstring (MySettings)" DbObjectName="boa.dbo.orders" DbObjectType="Table" FillMethodModifier="Public" FillMethodName="Fill" GenerateMethods="Both" GenerateShortCommands="True" GeneratorGetMethodName="GetData" GeneratorSourceName="Fill" GetMethodModifier="Public" GetMethodName="GetData" QueryType="Rowset" ScalarCallRetval="System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" UseOptimisticConcurrency="True" UserGetMethodName="GetData" UserSourceName="Fill">

Any idea what DbObjectName="boa.dbo.orders" needs to be changed to?

Thanks

Darren
 
Last edited:
Back
Top