xml to dataset to database

mowafy

Member
Joined
Oct 14, 2006
Messages
7
Programming Experience
Beginner
please any one tel me what is wrong i try to read an xml file to dataset and update an table on my database
her is my code
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ds [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataSet, Sql [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][SIZE=2], cmd [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlClient.SqlCommand[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dv [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataView[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dai [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlClient.SqlDataAdapter([/SIZE][SIZE=2][COLOR=#800000]"select * from Items"[/COLOR][/SIZE][SIZE=2], db)[/SIZE]
[SIZE=2][COLOR=#008000]'dai.Fill(ds, "Items")[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'ds.WriteXml(My.Application.Info.DirectoryPath & "\items.xml", XmlWriteMode.WriteSchema)[/COLOR][/SIZE]
[SIZE=2]ds = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataSet[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] System.IO.File.Exists([/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Application.Info.DirectoryPath & [/SIZE][SIZE=2][COLOR=#800000]"\items.xml"[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]ds.ReadXml([/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Application.Info.DirectoryPath & [/SIZE][SIZE=2][COLOR=#800000]"\items.xml"[/COLOR][/SIZE][SIZE=2], XmlReadMode.InferSchema)[/SIZE]
[SIZE=2]dv = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataView(ds.Tables(0))[/SIZE]
[SIZE=2][COLOR=#008000]'Dim dbb As New SqlClient.SqlBulkCopy(db)[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'dbb.DestinationTableName = "dbo.items"[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'dbb.WriteToServer(ds.Tables(0))[/COLOR][/SIZE]
[SIZE=2]cmd.Connection = db[/SIZE]
[SIZE=2]cmd.CommandText = [/SIZE][SIZE=2][COLOR=#800000]"INSERT INTO [dbo].[Items]"[/COLOR][/SIZE][SIZE=2] _[/SIZE]
[SIZE=2]& [/SIZE][SIZE=2][COLOR=#800000]" ([ItmCode],[ItmBarCode],[ItmArabName],[ItmEngName],[ItmPrice1],[ItmPrice2]"[/COLOR][/SIZE][SIZE=2] _[/SIZE]
[SIZE=2]& [/SIZE][SIZE=2][COLOR=#800000]" ,[ItmPrice3],[ItmPack],[ItmUnit],[ItmUnitQuantity],[ItmAverageCost],[ItmLastCost],[ItmOldAvrage])"[/COLOR][/SIZE][SIZE=2] _[/SIZE]
[SIZE=2]& [/SIZE][SIZE=2][COLOR=#800000]" values (@ItmCode,@ItmBarCode,@ItmArabName,@ItmEngName,@ItmPrice1,@ItmPrice2,@ItmPrice3,"[/COLOR][/SIZE][SIZE=2] _[/SIZE]
[SIZE=2]& [/SIZE][SIZE=2][COLOR=#800000]" @ItmPack,@ItmUnit,@ItmUnitQuantity,@ItmAverageCost,@ItmLastCost, @ItmOldAvrage) "[/COLOR][/SIZE]
[SIZE=2]cmd.Parameters.Add([/SIZE][SIZE=2][COLOR=#800000]"@ItmCode"[/COLOR][/SIZE][SIZE=2], SqlDbType.NVarChar, 25, [/SIZE][SIZE=2][COLOR=#800000]"ItmCode"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]cmd.Parameters.Add([/SIZE][SIZE=2][COLOR=#800000]"@itmBarCode"[/COLOR][/SIZE][SIZE=2], SqlDbType.NVarChar, 20, [/SIZE][SIZE=2][COLOR=#800000]"itmbarcode"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]cmd.Parameters.Add([/SIZE][SIZE=2][COLOR=#800000]"@ItmArabName"[/COLOR][/SIZE][SIZE=2], SqlDbType.NVarChar, 150, [/SIZE][SIZE=2][COLOR=#800000]"ItmarabName"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]cmd.Parameters.Add([/SIZE][SIZE=2][COLOR=#800000]"@ItmEngName"[/COLOR][/SIZE][SIZE=2], SqlDbType.NVarChar, 150, [/SIZE][SIZE=2][COLOR=#800000]"ItmEngName"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]cmd.Parameters.Add([/SIZE][SIZE=2][COLOR=#800000]"@ItmUnitQuantity"[/COLOR][/SIZE][SIZE=2], SqlDbType.Float, 8, [/SIZE][SIZE=2][COLOR=#800000]"ItmUnitQuantity"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]cmd.Parameters.Add([/SIZE][SIZE=2][COLOR=#800000]"@ItmPrice1"[/COLOR][/SIZE][SIZE=2], SqlDbType.Float, 8, [/SIZE][SIZE=2][COLOR=#800000]"ItmPrice1"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]cmd.Parameters.Add([/SIZE][SIZE=2][COLOR=#800000]"@ItmPrice2"[/COLOR][/SIZE][SIZE=2], SqlDbType.Float, 8, [/SIZE][SIZE=2][COLOR=#800000]"ItmPrice2"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]cmd.Parameters.Add([/SIZE][SIZE=2][COLOR=#800000]"@ItmPrice3"[/COLOR][/SIZE][SIZE=2], SqlDbType.Float, 8, [/SIZE][SIZE=2][COLOR=#800000]"ItmPrice3"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]cmd.Parameters.Add([/SIZE][SIZE=2][COLOR=#800000]"@ItmPack"[/COLOR][/SIZE][SIZE=2], SqlDbType.SmallInt, 4, [/SIZE][SIZE=2][COLOR=#800000]"ItmPack"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]cmd.Parameters.Add([/SIZE][SIZE=2][COLOR=#800000]"@ItmUnit"[/COLOR][/SIZE][SIZE=2], SqlDbType.NVarChar, 20, [/SIZE][SIZE=2][COLOR=#800000]"ItmUnit"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]cmd.Parameters.Add([/SIZE][SIZE=2][COLOR=#800000]"@ItmAverageCost"[/COLOR][/SIZE][SIZE=2], SqlDbType.Float, 8, [/SIZE][SIZE=2][COLOR=#800000]"ItmAverageCost"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]cmd.Parameters.Add([/SIZE][SIZE=2][COLOR=#800000]"@ItmLastCost"[/COLOR][/SIZE][SIZE=2], SqlDbType.Money, 8, [/SIZE][SIZE=2][COLOR=#800000]"ItmLastCost"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]cmd.Parameters.Add([/SIZE][SIZE=2][COLOR=#800000]"@ItmOldAvrage"[/COLOR][/SIZE][SIZE=2], SqlDbType.Money, 8, [/SIZE][SIZE=2][COLOR=#800000]"ItmOldAvrage"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] lm [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = ds.Tables([/SIZE][SIZE=2][COLOR=#800000]"items"[/COLOR][/SIZE][SIZE=2]).Rows.Count[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] da [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlClient.SqlDataAdapter[/SIZE]
[SIZE=2][COLOR=#008000]'da.Fill(ds)[/COLOR][/SIZE]
[SIZE=2]da.TableMappings.Add([/SIZE][SIZE=2][COLOR=#800000]"Items"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"Items"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]da.AcceptChangesDuringUpdate = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2]da.InsertCommand = cmd[/SIZE]
[SIZE=2]da.Update(ds.Tables(0))[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
 
plz explain ur question...

Hi mowafy,
can u plz explain your program requirement and also the error you face when compile this code. and you have not mention about XML file structure,, is this file created by 'ds.WriteXml(My.Application.Info.DirectoryPath & "\items.xml", XmlWriteMode.WriteSchema)
this commented code? if yes then please uncomment these lines in the post.
After that i can be able to answer your question.
Thanx
 
hi akhhttar
my program is to transfer data from one place to another
i read the data on dataset and then write to xml with xmlwritemode.writeschema

from my code i have that error

Prepared statement '(@ItmCode nvarchar(25),@itmBarCode nvarchar(20),@ItmArabName nva' expects parameter @ItmUnitQuantity, which was not supplied.
 
VB.NET:
cmd.Parameters.Add("@ItmCode", SqlDbType.NVarChar, 25, "ItmCode")
cmd.Parameters.Add("@itmBarCode", SqlDbType.NVarChar, 20, "itmbarcode")
cmd.Parameters.Add("@ItmArabName", SqlDbType.NVarChar, 150, "ItmarabName")
cmd.Parameters.Add("@ItmEngName", SqlDbType.NVarChar, 150, "ItmEngName")
cmd.Parameters.Add("@ItmPrice1", SqlDbType.Float, 8, "ItmPrice1")
cmd.Parameters.Add("@ItmPrice2", SqlDbType.Float, 8, "ItmPrice2")
cmd.Parameters.Add("@ItmPrice3", SqlDbType.Float, 8, "ItmPrice3")
cmd.Parameters.Add("@ItmPack", SqlDbType.SmallInt, 4, "ItmPack")
cmd.Parameters.Add("@ItmUnit", SqlDbType.NVarChar, 20, "ItmUnit")
cmd.Parameters.Add("@ItmUnitQuantity", SqlDbType.Float, 8, "ItmUnitQuantity")
cmd.Parameters.Add("@ItmAverageCost", SqlDbType.Float, 8, "ItmAverageCost")
cmd.Parameters.Add("@ItmLastCost", SqlDbType.Money, 8, "ItmLastCost")
cmd.Parameters.Add("@ItmOldAvrage", SqlDbType.Money, 8, "ItmOldAvrage")

Change the parameters to the above code. And check all the casings of the last argument against the names of the columns in the table.
 
Back
Top