0

I used the following T-SQL to insert data to an application which is build by C# Some records works fine but some give the error below.

enter image description here

INSERT INTO [MITESTCO].[dbo].[MIQSUP] ([itemId], [suplProdCode], [suplId])  
    SELECT
        [itemId], [suplId], [suplProdCode]  
    FROM 
        [table 28]   
    WHERE  
        [itemId] NOT IN (SELECT [itemId] FROM [MIQSUP]);

See the end of this message for details on invoking just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text ************** System.InvalidCastException: Conversion from type 'DBNull' to type 'String' is not valid. at Microsoft.VisualBasic.CompilerServices.Conversions.ToString(Object Value) at MISys.Client.MIUltraDataGrid.MIUltraDataGrid.MIUltraDataGrid_InitializeRow(Object sender, InitializeRowEventArgs e) at Infragistics.Win.UltraWinGrid.InitializeRowEventHandler.Invoke(Object sender, InitializeRowEventArgs e) at Infragistics.Win.UltraWinGrid.UltraGrid.OnInitializeRow(InitializeRowEventArgs e) at Infragistics.Win.UltraWinGrid.UltraGrid.FireEvent(GridEventIds id, EventArgs e) at Infragistics.Win.UltraWinGrid.UltraGrid.FireInitializeRow(InitializeRowEventArgs e) at Infragistics.Win.UltraWinGrid.UltraGridRow.FireInitializeRow() at Infragistics.Win.UltraWinGrid.RowsCollection.FireInitializeRow(IList rows) at Infragistics.Win.UltraWinGrid.RowsCollection.InitNonGroupByRows(IList fireInitializeRowOnTheseRows) at Infragistics.Win.UltraWinGrid.RowsCollection.SyncRowsHelper(IList boundList) at Infragistics.Win.UltraWinGrid.RowsCollection.SyncRows() at Infragistics.Win.UltraWinGrid.RowsCollection.EnsureNotDirty() at Infragistics.Win.UltraWinGrid.RowsCollection.GetEnumerator() at MISys.Client.MIUltraDataGrid.MIUltraDataGrid.RefreshGrid() at MISysForms.MIDetailsTemplate.RefreshGrids(Control myparent) at MISysForms.MIDetailsTemplate.RefreshGrids(Control myparent) at MISysForms.MIDetailsTemplate.RefreshGrids(Control myparent) at MISysForms.MIDetailsTemplate.RefreshGrids(Control myparent) at MISysForms.MIDetailsTemplate.RecordChanged() at MISysForms.MIItemDetails.RecordChanged() at MISysForms.MIDetailsTemplate.GetPreviousRecord() at MISysForms.MIDetailsTemplate.UltraToolbarsManager1_ToolClick(Object sender, ToolClickEventArgs e) at Infragistics.Win.UltraWinToolbars.UltraToolbarsManager.OnToolClick(ToolClickEventArgs e) at ....

6
  • 4
    How about sharing the actual code you are using? Commented May 19, 2016 at 20:59
  • If you click on details does it tell you more specifically where that error is coming from? Commented May 19, 2016 at 21:04
  • @TimFreese yes i add at the bottom of my questions Commented May 19, 2016 at 21:29
  • Here is the problem. You posted a dotnet error message and the only code you have shown us is a query. You can't get that error message from sql. You need to show us the dotnet code if you want any real chance at finding solution. Commented May 19, 2016 at 21:34
  • @SeanLange sorry, I dont have access to the .Net part Commented May 19, 2016 at 21:37

3 Answers 3

2

Ask your datarow if the value isn't null, for example, then you can cast it fine:

object Value = dataRow[columnName];
if (Value != DBNull.Value)
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the above answer or if you dont have access to the code, change your SELECT to insert blank instead of null's, assuming you have a SQL Server

INSERT INTO [MITESTCO].[dbo].[MIQSUP] ([itemId], [suplProdCode], [suplId])  
SELECT
    IsNull([itemId],''), IsNUll([suplId], ''), ISNull([suplProdCode],'')  
FROM 
    [table 28]   
WHERE  
    [itemId] NOT IN (SELECT [itemId] FROM [MIQSUP]);

Comments

0

One of your columns is returning NULL value and as indicated in stack trace, calling ToString() function raise that error. You can rewrite your SELECT statement using TSQL, ISNULL function.

INSERT INTO [MITESTCO].[dbo].[MIQSUP] ([itemId], [suplProdCode], [suplId])  
SELECT
    [itemId], [suplId], ISNULL([suplProdCode],'') as suplProdCode
FROM 
    [table 28]   
WHERE  
    [itemId] NOT IN (SELECT [itemId] FROM [MIQSUP]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.