2009-10-16 65 views
0

嗨,朋友們,希望大家都做得很好。我使用vb.net將數據從xml文件保存到sql server數據庫時遇到了一個問題,即"Conversion from type 'DBNull' to type 'String' is not valid"。但並不總是得到錯誤可能是基於xml文件。下面是我的一個xml文件。我曾經隨機生成xml文件。使用數據表將xml數據載入數據庫

<Jobs> 
- <Job ID="895562" PositionID="2300056"> 
    <Title>Senior Network Administrator</Title> 
- <Summary> 
- <![CDATA[ Join a global leader in an exciting Australia and New Zealand role. As the Leader of Technical Support you will be responsible for mentoring and managing a technical team striving to become the technical centre of excellence in the region. A hands on role, you will not only be the technical expert but you will work along side the sales team and directly contribute to the growth of the business. Th 
    ]]> 
    </Summary> 
    <DateActive Date="2009-10-15T19:06:00-05:00">10/15/2009</DateActive> 
    <DateExpires Date="2009-11-14T19:06:00-05:00">11/14/2009</DateExpires> 
    <DateUpdated Date="2009-10-15 19:06:00">10/15/2009</DateUpdated> 
    <Country>xxxx</Country> 
    <State>xxx</State> 
    <City>xxx</City> 
    <PostalCode>888899</PostalCode> 
    <Min Value="90000">90,000.00</Min> 
    <Max Value="100000">100,000.00</Max> 
    <Type ID="1">Per Year</Type> 
    <Currency ID="4">xxx</Currency> 
    <BuilderFields /> 
    <DisplayOptions>4</DisplayOptions> 
    <AddressType>6</AddressType> 
    </Job> 
- <Job ID="84000527" PositionID="61383275"> 
    <Title>QA Test Analyst- Banking and Finance</Title> 
- <Summary> 
- <![CDATA[ Extensive background in QA testing within the finance sector essential ISEB Testing qualification essential About our Client Our client is a leading global banking organisation Job Description In this role you will be responsible for systems integration testing, Quality Assurance and Environment support for a wide variety of technology projects. Responsibilities will include: Working closely with d 
    ]]> 
    </Summary> 
    <DateActive Date="2009-10-15T18:31:11-05:00">10/15/2009</DateActive> 
    <DateExpires Date="2009-11-14T20:00:35-05:00">11/14/2009</DateExpires> 
    <DateUpdated Date="2009-10-15 20:01:00">10/15/2009</DateUpdated> 
    <Country>xxx</Country> 
    <City>xxx</City> 
    <PostalCode>8888</PostalCode> 
    <Min Value="90000">90,000.00</Min> 
    <Max Value="100000">100,000.00</Max> 
    <Type ID="1">Per Year</Type> 
    <Currency ID="4">xxx</Currency> 
    <CompanyName>Michael Page International</CompanyName> 
    <BuilderFields /> 
    <DisplayOptions /> 
    <AddressType>6</AddressType> 
    </Job> 
<Job ID="895562" PositionID="2300056"> 
    <Title>Senior Network Administrator</Title> 
- <Summary> 
- <![CDATA[ Join a global leader in an exciting Australia and New Zealand role. As the Leader of Technical Support you will be responsible for mentoring and managing a technical team striving to become the technical centre of excellence in the region. A hands on role, you will not only be the technical expert but you will work along side the sales team and directly contribute to the growth of the business. Th 
    ]]> 
    </Summary> 
    <DateActive Date="2009-10-15T19:06:00-05:00">10/15/2009</DateActive> 
    <DateExpires Date="2009-11-14T19:06:00-05:00">11/14/2009</DateExpires> 
    <DateUpdated Date="2009-10-15 19:06:00">10/15/2009</DateUpdated> 
    <Country>xxxx</Country> 
    <State>xxx</State> 
    <City>xxx</City> 
    <PostalCode>888899</PostalCode> 
    <Min Value="90000">90,000.00</Min> 
    <Max Value="100000">100,000.00</Max> 
    <Type ID="1">Per Year</Type> 
    <Currency ID="4">xxx</Currency> 
    <BuilderFields /> 
    <DisplayOptions>4</DisplayOptions> 
    <AddressType>6</AddressType> 
    </Job> 
</Jobs> 

請求。幫助我解決這個問題。提前致謝。 +++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++ =

Protected Sub lnkbtnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) 
    Dim fid, filename As String 
    fid = CType(sender.parent.controls(0).parent.parent.controls(0), TableCell).Text 
    filename = fid & ".xml" 


    Dim uploadDS As New DataSet 
    Dim tempDT As New DataTable 
    Dim i As Integer 
    Dim ConeFilePath As String = Nothing 

    ConeFilePath = "~/CareerOneModifiedFeeds/" & filename.ToString() 
    uploadDS.ReadXml(Server.MapPath(ConeFilePath)) 
    tempDT = uploadDS.Tables(0).DefaultView.ToTable 
    Dim dtRow As DataRow 
    Dim JobCount As Integer 
    Dim Count As Integer = 0 

    Dim strsaltype, strsalcur As String 
    Dim strMinSal, strMaxSal As String 

    For i = 0 To tempDT.Rows.Count - 1 
     Count = Count + 1 
     dtRow = tempDT.Rows(i) 
     'Code to get the check whether the job id already exists or not 
     JobCount = Dr.CheckJobIDExists(UserID, Trim(dtRow.Item("JOB_ID"))) 

     Dim FullLoc As String = Replace(dtRow.Item("COUNTRY"), "'", "''").Trim() + "-" + Replace(dtRow.Item("STATE"), "'", "''").Trim() 
     If dtRow.Item("CITY") <> "" Then 
      FullLoc = FullLoc + "-" + Replace(dtRow.Item("CITY"), "'", "''").Trim() 
     End If 



     If JobCount = 1 Then 

      ' Update the record aganist that jobid 
      DR.UpdateExistingCareerOneJobs_XML(UserID, Trim(dtRow.Item("JOB_ID")), Trim(Replace(dtRow.Item("TITLE"), "'", "''")), _ 
      Trim(Replace(dtRow.Item("Summary"), "'", "''")), Trim(Replace(dtRow.Item("DateActive"), "'", "''")), _ 
      Trim(Replace(dtRow.Item("DateExpire"), "'", "''")), FullLoc, Trim(Replace(dtRow.Item("COUNTRY"), "'", "''")), _ 
      Trim(Replace(dtRow.Item("STATE"), "'", "''")), Trim(Replace(dtRow.Item("CITY"), "'", "''")), Trim(Replace(dtRow.Item("PostalCode"), "'", "''")), _ 
      Trim(Replace(dtRow.Item("BuilderFields"), "'", "''")), Trim(Replace(dtRow.Item("DisplayOptions"), "'", "''")), _ 
      Trim(Replace(dtRow.Item("AddressType"), "'", "''")), Trim(Replace(dtRow.Item("CompanyName"), "'", "''")), _ 
      Trim(Replace(dtRow.Item("PositionID"), "'", "''")), Trim(Replace(dtRow.Item("SalMin"), "'", "''")), Trim(Replace(dtRow.Item("SalMax"), "'", "''")), _ 
      Trim(Replace(dtRow.Item("SalType"), "'", "''")), Trim(Replace(dtRow.Item("SalCurrency"), "'", "''")), Replace(JobCat, "'", "''"), DateTime.Now()) 

     Else 
      ' Insert record new jobid 

        Dim z As Integer = DR.InsertCareeroneJobs_XML(UserID, Trim(dtRow.Item("JOB_ID")), Trim(Replace(dtRow.Item("TITLE"), "'", "''")), _ 
      Trim(Replace(dtRow.Item("Summary"), "'", "''")), Trim(Replace(dtRow.Item("DateActive"), "'", "''")), _ 
      Trim(Replace(dtRow.Item("DateExpire"), "'", "''")), FullLoc, Trim(Replace(dtRow.Item("COUNTRY"), "'", "''")), _ 
      Trim(Replace(dtRow.Item("STATE"), "'", "''")), Trim(Replace(dtRow.Item("CITY"), "'", "''")), _ 
      Trim(dtRow.Item("PostalCode")), Trim(Replace(dtRow.Item("BuilderFields"), "'", "''")), _ 
      Trim(Replace(Convert.ToString(dtRow.Item("DisplayOptions")), "'", "''")), Trim(Replace(dtRow.Item("AddressType"), "'", "''")), _ 
      Trim(Replace(dtRow.Item("CompanyName"), "'", "''")), Trim(Replace(dtRow.Item("PositionID"), "'", "''")), Trim(strsaltype.ToString()), _ 
      Trim(strsalcur.ToString()), Trim(Replace(Convert.ToString(JobCat), "'", "''")), strMinSal, strMaxSal, DateTime.Now()) 
      ' Trim(Replace(dtRow.Item("PositionID"), "'", "''")),Trim(strsaltype.ToString()), Trim(strsalcur.ToString()), _ 

      Response.Write(z) 
     End If 

    Next i 
End Sub 
+0

請發佈您用於將XML文件加載到DataTable中的代碼。 – 2009-10-16 06:37:21

回答

0

你可以嘗試以下storedproc在那裏你可以發送XML文件的XML內容插入到表中。訪問這個link

SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

CREATE PROCEDURE JobsBulkInsertXML @items xml 

    AS 
    DECLARE @hDoc int 
    exec sp_xml_preparedocument @hDoc OUTPUT, @items 

    Insert Into absorbentorder 
    SELECT dpc, absorbentcode, quantity 
    FROM OPENXML (@hDoc, '/absorbentitems',1) 
    WITH (dpc char(15), absorbentcode char(15), quantity char(15)) XMLItems 

    EXEC sp_xml_removedocument @hDoc 


    GO 
+0

我建議**不要**再使用NTEXT(或TEXT或IMAGE) - 它們已被棄用。改用VARCHAR(MAX),NVARCHAR(MAX)或VARBINARY(MAX)。 – 2009-10-16 07:43:23

+0

你可以用@items xml替換@items nText – Himadri 2009-10-16 09:42:33

0

我不認爲這是與你的SQL代碼的一個問題 - 但是你有可能包含空字段數據行打交道時需要小心你的vb.net代碼:

Trim(Replace(dtRow.Item("DateExpire"), "'", "''")) 
Trim(Replace(dtRow.Item("COUNTRY"), "'", "''")) 

如果dtRow.Item("COUNTRY")(或您正在使用的那個)爲空/ NULL,所有這些對Replace的調用可能會落在他們的鼻子上。

您需要更謹慎地應用這些方法並首先檢查DBNull.Value!

創建一個函數像這樣(在C#中 - 應該沒有問題,轉換,爲VB.NET):

public string ReplaceString(DataRow row, string fieldname) 
{ 
    // first make sure the value of the field in the row is not NULL ! 
    if(!row.IsNull(fieldname)) 
    { 
     // only if NOT null, do the replace 
     return row[fieldname].Replace("'", "''").Trim(); 
    } 

    // if the field is DBNull, return an empty string 
    return string.Empty; 
} 

,然後用它來代替你的顯式調用來Trim(Replace(...))

Marc

相關問題