Datatype in MS SQL: [Quantity] DECIMAL (18, 6) NULL,
iGrid: Unbound mode.
C# in Visual Studio 2017.
Data from MS SQL to iGrid works well.
Generate columns, set the ValueTypes to what they are in SQL. nvarchar, int32 and Decimal(18,6). The Decimal fields in iGrid comes in with six decimals.
Problem? Saving data from iGrid to MS SQL results in: System.Data.SqlClient.SqlException "Error converting data type nvarchar to numeric."
At first glance this is just 'trying to input data into the wrong database field' - but no. Going into iGrid and copying the Decimal values into their own cells results in everything working fine. Write "22,22" or something in them all and everything works fine. What is the actual problem here?
a) Is there, for example, some way to tell the iGrid cell that it should use Decimal(18,6) instead of some generic 'Decimal' type? Would there be any point in doing so?
b) Can one get better (or more elaborate) error descriptions somehow?
c) This is just test code, even the database definitions are a bit up in the air, but still, I just recently started with C#. It probably shows. If there are any obviously better ways to do stuff, please do feel free to elaborate.
CREATE TABLE [dbo].[BULK] (
[ItemCode] NVARCHAR (50) NOT NULL,
[PieceCode] NVARCHAR (50) NOT NULL,
[InstCode] NVARCHAR (3) NOT NULL,
[PieceName] NVARCHAR (70) NULL,
[SectionCode] NVARCHAR (20) NOT NULL,
[AreaCode] NVARCHAR (20) NULL,
[DiscCode] NVARCHAR (4) NOT NULL,
[Date] NVARCHAR (20) NULL,
[MadeBy] NVARCHAR (10) NULL,
[PBSCode] NVARCHAR (5) NULL,
[SystemCode1] NVARCHAR (20) NULL,
[SystemCode2] NVARCHAR (20) NULL,
[SystemCode3] NVARCHAR (20) NULL,
[SystemCode4] NVARCHAR (20) NULL,
[SystemCode5] NVARCHAR (20) NULL,
[SystemCode6] NVARCHAR (20) NULL,
[SystemCode7] NVARCHAR (20) NULL,
[SystemCode8] NVARCHAR (20) NULL,
[SystemCode9] NVARCHAR (20) NULL,
[ProcCode] NVARCHAR (12) NULL,
[MatrCode] NVARCHAR (20) NULL,
[LiftCode] NVARCHAR (6) NULL,
[CORCode] NVARCHAR (7) NULL,
[NoOff] INT NULL,
[Quantity] DECIMAL (18, 6) NULL,
[UnitWeight] DECIMAL (18, 6) NOT NULL,
[UnitContent] DECIMAL (18, 6) NOT NULL,
[WeightStatusCode] NVARCHAR (6) NOT NULL,
[ConstrCode] NVARCHAR (2) NULL,
[Remark] NVARCHAR (100) NULL,
[XCoGlo] DECIMAL (18, 6) NOT NULL,
[YCoGlo] DECIMAL (18, 6) NOT NULL,
[ZCoGlo] DECIMAL (18, 6) NOT NULL,
[CategoryCode] NVARCHAR (1) NULL,
[PriceCode] NVARCHAR (50) NULL,
[Code1] NVARCHAR (50) NULL,
[Code2] NVARCHAR (50) NULL,
[MHC] NVARCHAR (3) NULL,
CONSTRAINT [PK_BULK] PRIMARY KEY CLUSTERED ([ItemCode] ASC, [PieceCode] ASC, [InstCode] ASC)
);
private void SaveDataButton_Click(object sender, EventArgs e)
{
int row = 0;
int numberOfItems = rowNumber;
string MS_SQL_Project_ConnectionString = Startmenu.MS_SQL_Project_ConnectionString;
string test;
using (SqlConnection connection = new SqlConnection(MS_SQL_Project_ConnectionString))
{
string sql = "UPDATE [BULK] SET PieceName = @PieceName, " +
"SectionCode = @SectionCode, AreaCode = @AreaCode, DiscCode = @DiscCode, Date = @Date, MadeBy = @MadeBy, " +
"PBSCode = @PBSCode, SystemCode1 = @SystemCode1, SystemCode2 = @SystemCode2, SystemCode3 = @SystemCode3, " +
"SystemCode4 = @SystemCode4, SystemCode5 = @SystemCode5, SystemCode6 = @SystemCode6, SystemCode7 = @SystemCode7, " +
"SystemCode8 = @SystemCode8, SystemCode9 = @SystemCode9, ProcCode = @ProcCode, MatrCode = @MatrCode, " +
"LiftCode = @LiftCode, CORCode = @CORCode, NoOff = @NoOff, Quantity = @Quantity, UnitWeight = @UnitWeight, " +
"UnitContent = @UnitContent, WeightStatusCode = @WeightStatusCode, ConstrCode = @ConstrCode, Remark = @Remark, " +
"XCoGlo = @XCoGlo, YCoGlo = @YCoGlo, ZCoGlo = @ZCoGlo, CategoryCode = @CategoryCode, PriceCode = @PriceCode, " +
"Code1 = @Code1, Code2 = @Code2, MHC = @MHC " +
"WHERE ItemCode = @ItemCode AND PieceCode = @PieceCode AND InstCode = @InstCode;";
connection.Open();
Application.DoEvents();
using (SqlCommand command = new SqlCommand(sql, connection))
{
while (row < numberOfItems)
{
//command.Parameters.AddWithValue("@ItemCode", list[i].ItemCode);
command.Parameters.AddWithValue("@ItemCode", fGrid.Cells[row, 0].Value);
//command.Parameters.AddWithValue("@PieceCode", list[i].PieceCode);
command.Parameters.AddWithValue("@PieceCode", fGrid.Cells[row, 1].Value);
//command.Parameters.AddWithValue("@InstCode", list[i].InstCode);
command.Parameters.AddWithValue("@InstCode", fGrid.Cells[row, 2].Value);
//command.Parameters.AddWithValue("@PieceName", list[i].PieceName);
command.Parameters.AddWithValue("@PieceName", fGrid.Cells[row, 3].Value);
command.Parameters.AddWithValue("@SectionCode", fGrid.Cells[row, 4].Value);
command.Parameters.AddWithValue("@AreaCode", fGrid.Cells[row, 5].Value);
command.Parameters.AddWithValue("@DiscCode", fGrid.Cells[row, 6].Value);
command.Parameters.AddWithValue("@Date", fGrid.Cells[row, 7].Value);
command.Parameters.AddWithValue("@MadeBy", fGrid.Cells[row, 8].Value);
command.Parameters.AddWithValue("@PBSCode", fGrid.Cells[row, 9].Value);
//command.Parameters.AddWithValue("@AreaCode", fGrid.Cells[row, 9].Value);
command.Parameters.AddWithValue("@SystemCode1", fGrid.Cells[row, 10].Value);
command.Parameters.AddWithValue("@SystemCode2", fGrid.Cells[row, 11].Value);
command.Parameters.AddWithValue("@SystemCode3", fGrid.Cells[row, 12].Value);
command.Parameters.AddWithValue("@SystemCode4", fGrid.Cells[row, 13].Value);
command.Parameters.AddWithValue("@SystemCode5", fGrid.Cells[row, 14].Value);
command.Parameters.AddWithValue("@SystemCode6", fGrid.Cells[row, 15].Value);
command.Parameters.AddWithValue("@SystemCode7", fGrid.Cells[row, 16].Value);
command.Parameters.AddWithValue("@SystemCode8", fGrid.Cells[row, 17].Value);
command.Parameters.AddWithValue("@SystemCode9", fGrid.Cells[row, 18].Value);
command.Parameters.AddWithValue("@ProcCode", fGrid.Cells[row, 19].Value);
command.Parameters.AddWithValue("@MatrCode", fGrid.Cells[row, 20].Value);
command.Parameters.AddWithValue("@LiftCode", fGrid.Cells[row, 21].Value);
command.Parameters.AddWithValue("@CORCode", fGrid.Cells[row, 22].Value);
test = fGrid.Cols[23].CellStyle.ValueType.Name;
command.Parameters.AddWithValue("@NoOff", fGrid.Cells[row, 23].Value);
test = fGrid.Cols[24].CellStyle.ValueType.Name;
command.Parameters.AddWithValue("@Quantity", fGrid.Cells[row, 24].Value);
test = fGrid.Cols[25].CellStyle.ValueType.Name;
command.Parameters.AddWithValue("@UnitWeight", fGrid.Cells[row, 25].Value);
command.Parameters.AddWithValue("@UnitContent", fGrid.Cells[row, 26].Value);
command.Parameters.AddWithValue("@WeightStatusCode", fGrid.Cells[row, 27].Value);
command.Parameters.AddWithValue("@ConstrCode", fGrid.Cells[row, 28].Value);
command.Parameters.AddWithValue("@Remark", fGrid.Cells[row, 29].Value);
command.Parameters.AddWithValue("@XCoGlo", fGrid.Cells[row, 30].Value);
command.Parameters.AddWithValue("@YCoGlo", fGrid.Cells[row, 31].Value);
command.Parameters.AddWithValue("@ZCoGlo", fGrid.Cells[row, 32].Value);
command.Parameters.AddWithValue("@CategoryCode", fGrid.Cells[row, 33].Value);
command.Parameters.AddWithValue("@PriceCode", fGrid.Cells[row, 34].Value);
command.Parameters.AddWithValue("@Code1", fGrid.Cells[row, 35].Value);
command.Parameters.AddWithValue("@Code2", fGrid.Cells[row, 36].Value);
command.Parameters.AddWithValue("@MHC", fGrid.Cells[row, 37].Value);
command.ExecuteNonQuery();
command.Parameters.Clear();
//command.ExecuteNonQueryAsync(); // Faster? Lots at the same time? Not tested.
row++;
ItemCountField.Text = Convert.ToString(row);
ItemCountField.Refresh();
}
}
connection.Close();
}
}
private void BULK_igrid_Excel_Load(object sender, EventArgs e)
{
string MS_SQL_Project_ConnectionString = Startmenu.MS_SQL_Project_ConnectionString;
string headerName = "Error: Not a header";
Type columnDataType;
string headerType;
rowNumber = 0;
using (SqlConnection projectConnection = new SqlConnection(MS_SQL_Project_ConnectionString))
{
projectConnection.Open();
using (SqlCommand command = new SqlCommand("SELECT * FROM [BULK];", projectConnection))
{
SqlDataReader reader = command.ExecuteReader();
if (reader != null) // Set Column Headers
{
for (int i = 0; i < reader.FieldCount; i++)
{
headerName = reader.GetName(i);
columnDataType = reader.GetFieldType(i);
headerType = columnDataType.Name;
fGrid.Cols.Add(headerName);
fGrid.Cols[i].CellStyle.ValueType = columnDataType;
if (headerType == "Decimal")
{
i = i + 1 - 1;
}
if (fGrid.Cols[i].CellStyle.ValueType == null)
{
i = i +1 -1; // ERROR!!!
}
if (headerType == "nvarchar") // Default value = ""
{
i = i + 1 - 1; // What is situation here?
//fGrid.Cols[i].CellStyle.
}
}
}
fGrid.DefaultRow.Height = fGrid.GetPreferredRowHeight(true, true); // Dynamic vis a vis DPI
fGrid.Rows.Count = 1000;
fGrid.BeginUpdate();
while (reader.Read()) // Set Row Values
{
for (int i = 0; i < reader.FieldCount; i++)
{
fGrid.Cells[rowNumber, i].Value = reader[i].ToString(); // One item in row read into grid
}
rowNumber++; // Row complete, next row. Also total number of Rows in grid.
if ((rowNumber % 1000) == 0) // Modulo
{
fGrid.Rows.Count = rowNumber + 1000;
}
}
reader.Close();
fGrid.Rows.Count = rowNumber;
//PopulateGrid(rowNumber);
fGrid.EndUpdate();
RowNumberField.Text = Convert.ToString(rowNumber);
RowNumberField.Refresh();
}
projectConnection.Close();
}
}
Edited by user
2017-10-03T10:45:15Z
|
Reason: Clarification