Help with linq to sql insert error - c#

I have a database table with all columns to allow nulls for testing purposes. Between all of my columns I have int, varchar or bit datatypes. When I try to submit the form I get the following error message:
Value was either too large or too small for an Int32.
Here is the code:
using (storeDataContext db = new storeDataContext())
{
db.Dealerssses.InsertOnSubmit(new Dealersss
{
AppFName = txtFName.Text,
AppLName = txtLName.Text,
AppTitle = ddlTitles.SelectedItem.Text,
AppPhone = Convert.ToInt32(txtPhone.Text),
AppEmail = txtEmail.Text,
AppAddress = txtAddress.Text,
AppCity = txtCity.Text,
AppState = txtState.Text,
AppZip = Convert.ToInt32(txtZip.Text),
BusName = txtBusName.Text,
BusCA = Convert.ToInt32(txtBusResale.Text),
BusContact = txtBusContact.Text,
BusDBA = txtDBA.Text,
BusEIN = Convert.ToInt32(txtBusEIN.Text),
BusEmail = txtBusEmail.Text,
BusFax = Convert.ToInt32(txtBusFax.Text),
BusMonth = ddlMonthStart.SelectedItem.Text,
BusNumEmployees = Convert.ToInt32(txtBusEmployees.Text),
BusPhone = Convert.ToInt32(txtBusPhone.Text),
BusYear = int.Parse(txtYearStart.Text),
Active = false
});
db.SubmitChanges();
};

Int32.MaxValue is 2,147,483,647, which is only 10 digits long.
Your values are too large for an Int32.
Your Phone, Fax, ZIP, and EIN fields should be strings (NVARCHARs), not numbers.

I'm betting it's the value for your phone number fields. Int32 is a 4 byte integer with a max value of 2147483647. Most phone numbers will overflow that.

My guess is it is on your phone #'s Int32's have a value of:
-2,147,483,648 to 2,147,483,647
So if you have the phone # of 517111111 (5,171,111,111), you are too large.
You should use varchar/char for phone numbers.

Related

Reading TCURR table with RFC_READ_TABLE truncates the rate value

I'm trying to read data from SAP ECC using Microsoft .NET. For this, I am using the SAP Connector for Microsoft .NET 3.0 Following is the code to retrieve the data, I'm getting the results too. However, I found that the exchange rate value is having a * if it exceeds 7 characters.
ECCDestinationConfig cfg = new ECCDestinationConfig();
RfcDestinationManager.RegisterDestinationConfiguration(cfg);
RfcDestination dest = RfcDestinationManager.GetDestination("mySAPdestination");
RfcRepository repo = dest.Repository;
IRfcFunction testfn = repo.CreateFunction("RFC_READ_TABLE");
testfn.SetValue("QUERY_TABLE", "TCURR");
// fields will be separated by semicolon
testfn.SetValue("DELIMITER", ";");
// Parameter table FIELDS contains the columns you want to receive
// here we query 3 fields, FCURR, TCURR and UKURS
IRfcTable fieldsTable = testfn.GetTable("FIELDS");
fieldsTable.Append();
fieldsTable.SetValue("FIELDNAME", "FCURR");
fieldsTable.Append();
fieldsTable.SetValue("FIELDNAME", "TCURR");
fieldsTable.Append();
fieldsTable.SetValue("FIELDNAME", "UKURS");
fieldsTable.Append();
fieldsTable.SetValue("FIELDNAME", "GDATU");
// the table OPTIONS contains the WHERE condition(s) of your query
// several conditions have to be concatenated in ABAP syntax, for instance with AND or OR
IRfcTable optsTable = testfn.GetTable("OPTIONS");
var dateVal = 99999999 - 20190701;
optsTable.Append();
optsTable.SetValue("TEXT", "gdatu = '" + dateVal + "' and KURST = 'EURX'");
testfn.Invoke(dest);
Values are as follows:
How to get the full value without any truncation?
You just ran into the worst limitation of RFC_READ_TABLE.
Its error is to return field values based on internal length and truncating the rest, rather than using the output length. TCURR-UKURS is a BCD decimal packed field of length 9,5 (9 bytes = 17 digits, including 5 digits after the decimal point) and an output length of 12. Unfortunately, RFC_READ_TABLE outputs the result on 9 characters, so a value of 105.48000- takes 10 characters is too long, so ABAP default logic is to set the * overflow character on the leftmost character (*5.48000-).
Either you create another RFC-enabled function module at SAP/ABAP side, or you access directly the SAP database (classic RDBMS connected to SAP server).
Just an addition to Sandra perfect explanation about this issue. Yes, the only solution here would be writing a custom module for fetching remote records.
If you don't want to rewrite it from scratch the simplest solution would be to copy RFC_READ_TABLE into Z module and change line 137
FIELDS_INT-LENGTH_DST = TABLE_STRUCTURE-LENG.
to
FIELDS_INT-LENGTH_DST = TABLE_STRUCTURE-OUTPUTLEN.
This solves the problem.
UPDATE: try BAPI_EXCHANGERATE_GETDETAIL BAPI, it is RFC-enabled and reads rates correctly. The interface is quite self-explanatory, the only difference is that date should be in native format, not in inverted:
CALL FUNCTION 'BAPI_EXCHANGERATE_GETDETAIL'
EXPORTING
rate_type = 'EURO'
from_curr = 'USD'
to_currncy = 'EUR'
date = '20190101'
IMPORTING
exch_rate = rates
return = return.
Use BBP_RFC_READ_TABLE. It is still not the best but it does one thing right which RFC_READ_TABLE did not: one additional byte for the decimal sign.
No need to go through all the ordeal if you only look for patching the decimal issue.
This is the sample code used with SAP connector for .NET, let it be helpful for someone who looks for the same. Thanks for all those who helped.
var RateForDate = 20190701;
ECCDestinationConfig cfg = new ECCDestinationConfig();
RfcDestinationManager.RegisterDestinationConfiguration(cfg);
RfcDestination dest = RfcDestinationManager.GetDestination("mySAPdestination");
RfcRepository repo = dest.Repository;
IRfcFunction sapFunction = repo.CreateFunction("RFC_READ_TABLE");
sapFunction.SetValue("QUERY_TABLE", "TCURR");
// fields will be separated by semicolon
sapFunction.SetValue("DELIMITER", ";");
// Parameter table FIELDS contains the columns you want to receive
// here we query 3 fields, FCURR, TCURR and UKURS
IRfcTable fieldsTable = sapFunction.GetTable("FIELDS");
fieldsTable.Append();
fieldsTable.SetValue("FIELDNAME", "FCURR");
//fieldsTable.Append();
//fieldsTable.SetValue("FIELDNAME", "TCURR");
//fieldsTable.Append();
//fieldsTable.SetValue("FIELDNAME", "UKURS");
// the table OPTIONS contains the WHERE condition(s) of your query
// here a single condition, KUNNR is to be 0012345600
// several conditions have to be concatenated in ABAP syntax, for instance with AND or OR
IRfcTable optsTable = sapFunction.GetTable("OPTIONS");
var dateVal = 99999999 - RateForDate;
optsTable.Append();
optsTable.SetValue("TEXT", "gdatu = '" + dateVal + "' and KURST = 'EURX'");
sapFunction.Invoke(dest);
var companyCodeList = sapFunction.GetTable("DATA");
DataTable Currencies = companyCodeList.ToDataTable("DATA");
//Add additional column for rates
Currencies.Columns.Add("Rate", typeof(double));
//------------------
sapFunction = repo.CreateFunction("BAPI_EXCHANGERATE_GETDETAIL");
//rate type of your system
sapFunction.SetValue("rate_type", "EURX");
sapFunction.SetValue("date", RateForDate.ToString());
//Main currency of your system
sapFunction.SetValue("to_currncy", "EUR");
foreach (DataRow item in Currencies.Rows)
{
sapFunction.SetValue("from_curr", item[0].ToString());
sapFunction.Invoke(dest);
IRfcStructure impStruct = sapFunction.GetStructure("EXCH_RATE");
item["Rate"] = impStruct.GetDouble("EXCH_RATE_V");
}
dtCompanies.DataContext = Currencies;
RfcDestinationManager.UnregisterDestinationConfiguration(cfg);

Oracle PLSQLAssociative array greater than 4000 characters c# issue

Save data in oracle database 11g using pl sql associative array with each String[] array in C# having greater than 4k characters. Following is code:
public Boolean InsertIntoDummy(String[] dataMasterDetail)
{
OracleParameter p1 = new OracleParameter();
p1.OracleDbType = OracleDbType.Varchar2;
p1.Direction = ParameterDirection.InputOutput;
p1.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
p1.Value = dataMasterDetail;
p1.Size = dataMasterDetail.Length;
p1.ArrayBindSize = Enumerable.Repeat(12000,dataMasterDetail.Length).ToArray();
}
I expect that when my dataMasterDetail has greater than 4k characters for each row then data must be processed without any error.
Error received-
ORA-01460: unimplemented or unreasonable conversion requested
Thanks,
Sachin

Insert Currency into Access Database C#?

I want to insert currency in access data I am using datatype currency with general number format here is my c# code
decimal price = decimal.Parse(vCCY_Rate.ToString(), System.Globalization.NumberStyles.Currency);
SCmd.Parameters.AddWithValue("#CCY_Rate", price);
nReturnValue = (int)SCmd.ExecuteNonQuery();
where vCCY_Rate is double but this always store 1 in database and some time 0
can you tell me how to store currency in database access using c#. I have data of precion 6 that is also mentioned in currency datatype with general format and presicion
SQL Command
OleDbConnection SConnection = new OleDbConnection();
OleDbCommand SCmd = new OleDbCommand();
Save_Main("INSERT into tblOprCurrency_Rate ([CCY_ID_From], [CCY_ID_To] , [CCY_Active] , [CCY_Rate] ,[CCY_Chr]) VALUES (? , ? , ? ,? ,? )", cmbCurrency_From.SelectedItem.Value, cmbCurrency_To.SelectedItem.Value, Convert.ToDouble(txtCCY_Rate.Text), chkCCY_Active.Checked, 0, hdfSr_ID.Value);
public int Save_Main(string vQuery, string vCCY_ID_From, string vCCY_ID_To, double vCCY_Rate, bool bCCY_Active, int vIsUpdate, string vCCY_SrNo)
{
sErrorCode = "";
int nReturnValue = -1;
try
{
decimal price = (decimal)vCCY_Rate;
// decimal price = decimal.Parse(vCCY_Rate.ToString(), System.Globalization.NumberStyles.Currency);
SCmd.CommandText = vQuery;
SCmd.CommandType = CommandType.Text;
SCmd.Parameters.AddWithValue("#CCY_ID_From", vCCY_ID_From);
SCmd.Parameters.AddWithValue("#CCY_ID_To", vCCY_ID_To);
SCmd.Parameters.AddWithValue("#CCY_Rate", price);
SCmd.Parameters.AddWithValue("#CCY_Active", bCCY_Active);
SCmd.Parameters.AddWithValue("#CCY_Chr", Convert.ToDouble(0));
SCmd.Parameters.AddWithValue("#CCY_SrNo", vCCY_SrNo);
SCmd.Connection = SConnection;
SCmd.CommandTimeout = pSQL_CommandTimeOut;
SDataAdapter.SelectCommand = SCmd;
SConnection.Open();
nReturnValue = (int)SCmd.ExecuteNonQuery();
}
CCY_Rate is currency datatype and generat format with 6 decimal places in ACCESS
You are using named parameters in your code & positional parameters in your query - Rate & Active are in the wrong order.
Also you have added 6 parameters, but only 5 positional (question mark) parameters.

Converting Binary(16) to int with same result in C#

I have Binary(16) column in table 'Chip' with value 0xE1FC2E6F8674B7B9045C1104F9124C48 and in another table i have column chip_i which is type of integer that has the same value (but in int) = -116241336.
Im using SQL Server 2012.
How can i convert 0xE1FC2E6F8674B7B9045C1104F9124C48 to -116241336 in C#?
I tried to convert it like this:
string hexString = "0xE1FC2E6F8674B7B9045C1104F9124C48";
byte[] hexByte = Encoding.ASCII.GetBytes(hexString);
var chip_i = BitConverter.ToInt32(hexByte, 0);
but the result is 826636336
-116241336 is simply the last 4 bytes treated as a raw little-endian integer; 0xF9124C48. So: just use the last 4 bytes as is. No need for ASCII:
int chip_i = Convert.ToInt32(hexString.Substring(hexString.Length - 8, 8), 16);

How to convert decimal to hex using c#?

I have a program written in Delphi is converting 11 to 0xB and 28 to 0x1c. I tried to convert 11 (decimal to Hex) in c# using this:-
var deciValue01 = 11;
var deciValue02 = 28;
var deciValue03 = 13;
System.Diagnostics.Debug.WriteLine(string.Format("11 = {0:x}", deciValue01));
System.Diagnostics.Debug.WriteLine(string.Format("28 = {0:x}", deciValue02));
System.Diagnostics.Debug.WriteLine(string.Format("13 = {0:x}", deciValue03));
but the results I am getting is:-
11 = b
28 = 1c
13 = d
Wondering how to convert 11 to '0xB' and 28 to '0x1c' and 13 to '0xD'? Isn't it I need to change from Decimal to Hex?
You just need to use X to make it capital hex digits instead of lower case, and add the 0x yourself:
// Add using System.Diagnostics; at the top of the file... no need to
// explicitly qualify all your type names
Debug.WriteLine(string.Format("11 = 0x{0:X}", deciValue01));
Debug.WriteLine(string.Format("28 = 0x{0:X}", deciValue02));
Debug.WriteLine(string.Format("13 = 0x{0:X}", deciValue03));
Note that the deciValue01 values are neither "decimal" nor "hex" themselves. They're just numbers. The concept of "decimal" or "hex" only makes sense when you're talking about a textual representation, at least for integers. (It matters for floating point, where the set of representable types depends on the base used.)
Try This
int value = Convert.ToInt32(/*"HexValue"*/);
String hexRepresentation = Convert.ToString(value, 16);
It sounds like you want this...
var deciValue01 = 11;
var deciValue02 = 28;
var deciValue03 = 13;
System.Diagnostics.Debug.WriteLine(string.Format("11 = 0x{0:x}", deciValue01));
System.Diagnostics.Debug.WriteLine(string.Format("28 = 0x{0:x}", deciValue02));
System.Diagnostics.Debug.WriteLine(string.Format("13 = 0x{0:x}", deciValue03));

Categories

Resources