Соответствующий код: < /p>
Код: Выделить всё
int contactId = -1;
oString = "INSERT into {0}.Creditors (AccountID, DissectionId, Reference, FileAs, ABN, Created, Modified, GUID)"
+ " output INSERTED.ID"
+ " values (1, @dissectionId, ";
if(disbursement.trade_supplier.trust_id.Length > 0)
{
oString += "@reference, ";
}
else
{
oString += "null, ";
}
if(disbursement.trade_supplier.trading_name.Length > 0)
{
oString += "@name, ";
}
else
{
oString += "null, ";
}
if(disbursement.trade_supplier.business_number.Length > 0)
{
oString += "@abn, ";
}
else
{
oString += "null, ";
}
oString += " CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, @guid)";
oString = string.Format(oString, "[" + textBox1.Text + "].[dbo]");
using (SqlCommand insertCreditor = new SqlCommand(oString))
{
insertCreditor.Connection = myConnection;
insertCreditor.Parameters.Add("@dissectionId", SqlDbType.Int).Value = Convert.ToInt32(defaultDissectionID);
if (disbursement.trade_supplier.trust_id.Length > 0)
{
insertCreditor.Parameters.Add("@reference", SqlDbType.NVarChar, 8).Value = disbursement.trade_supplier.trust_id;
}
if (disbursement.trade_supplier.trading_name.Length > 0)
{
insertCreditor.Parameters.Add("@name", SqlDbType.NVarChar, 200).Value = disbursement.trade_supplier.trading_name;
}
if (disbursement.trade_supplier.business_number.Length > 0)
{
SqlParameter abn = new SqlParameter("@abn", SqlDbType.NVarChar, 14);
abn.Value = disbursement.trade_supplier.business_number;
MessageBox.Show(abn.GetType().ToString());
MessageBox.Show(abn.Value.ToString());
if (insertCreditor == null)
{
MessageBox.Show("InsertCreditor is null");
}
if (insertCreditor.Parameters == null)
{
MessageBox.Show("Parameters is null");
}
if(abn == null)
{
MessageBox.Show("null object sql parameter");
}
else
{
MessageBox.Show("sql parameter is not null");
if (insertCreditor.Parameters == null)
{
MessageBox.Show("Parameters collection is null?");
}
else
{
MessageBox.Show("Parameters collection is not null");
insertCreditor.Parameters.Add(abn);
}
}
}
if (disbursement.trade_supplier.guid.Length > 0)
{
insertCreditor.Parameters.Add("@guid", SqlDbType.UniqueIdentifier).Value = disbursement.trade_supplier.guid;
}
else
{
insertCreditor.Parameters.Add("@guid", SqlDbType.UniqueIdentifier).Value = new Guid();
}
}
Это вызывает nullreferenceecption: ссылка на объект не установлен на экземпляр объекта. INSERTCREDITOR.PARAMETERS - null.
Если я удалю этот раздел и просто использую там NULL, он пройдет его, но затем снова случается с другим значением в следующей вставке после этого.
переменные, показанные, когда я нарушаю точку на линейке,
.
Код: Выделить всё
abn {@abn} System.Data.SqlClient.SqlParameter
abn.Value "1223334555" object {string}
disbursement.trade_supplier {PSConsoleExtractor.TradeSupplier} PSConsoleExtractor.TradeSupplier
disbursement.trade_supplier.business_number "1223334555" string
insertCreditor.Parameters {System.Data.SqlClient.SqlParameterCollection} System.Data.SqlClient.SqlParameterCollection
this {PSConsoleExtractor.Form1, Text: PropertySafe Console Exporter (v 2.0.2.0)} PSConsoleExtractor.Form1
Подробнее здесь: https://stackoverflow.com/questions/327 ... collection
Мобильная версия