오류는 "데이터 유형 varchar를 숫자로 변환하는 동안 오류가 발생했습니다."라고 말합니다.
이것은 내 코드 세트입니다.
private void btnSearchCustomer_Click(object sender, EventArgs e)
{
//Get Customer Records
DataSet dsCustomer = new DataSet();
dsCustomer = GetRecords("Customers");
frmBasicSearch newSearch = new frmBasicSearch();
newSearch.myDataSet = dsCustomer;
newSearch.ShowDialog();
int myRowPosition = newSearch.myRowPosition;
if (myRowPosition != -1) //will display the value inside the textboxes
{
//concuntinated values
this.txtCustomerNo.Text = dsCustomer.Tables["Customers"].Rows[myRowPosition]["CustomerNo"].ToString();
this.txtCustomerName.Text = dsCustomer.Tables["Customers"].Rows[myRowPosition]["CustomerName"].ToString();
this.txtCustomerAddress.Text = dsCustomer.Tables["Customers"].Rows[myRowPosition]["CustomerAddress"].ToString();
groupProduct(true); //this will activate the buttons from the Product Section
}
cn.Close();
cn.Open();
SqlCommand cmdInsert = new SqlCommand();
cmdInsert.Connection = cn;
cmdInsert.Transaction = trnOrder;
cmdInsert.CommandType = CommandType.Text;
cmdInsert.CommandText =
"INSERT INTO ShoppingCart " +
"(OrderDate, CustomerNo, CustomerName, CustomerAddress, PurchaseOrderNo, AgentNo, AgentName, InvoiceNo, TotalAmount, OrderStatus) " +
"VALUES ('" +
dtpOrderDate.Value.Date.ToString() + "', '" +
txtCustomerNo.Text + "', '" +
txtCustomerName.Text + "', '" +
txtCustomerAddress.Text + "', '" +
DBNull.Value + "', '" +
DBNull.Value + "', '" +
DBNull.Value + "', '" +
DBNull.Value + "', '" +
DBNull.Value + "', '" +
"''Void'); " +
"SELECT TOP 1 ShoppingCartNo FROM ShoppingCart " +
"ORDER BY ShoppingCartNo DESC;";
cmdInsert.ExecuteNonQuery();
cn.Close();
}
강조 표시된 오류 부분은
int nShoppingCart = Convert.ToInt16(cmdInsert.ExecuteScalar().ToString());
문제가 어디에 있는지 알 수없는 것 같습니까? 도와 주셔서 감사합니다
여기 내 데이터 스키마가 있습니다.
ShoppingCartNo는 기본 키에 있으며 IS ID는 자동으로 기소됩니다 1
ExecuteScalar
SELECT 문을 실행하고 가장 먼저 반환 된 행의 첫 번째 열을 반환합니다. INSERT 문의 경우 ExecuteNonQuery를 사용하십시오. 삽입 된 줄 수를 포함하는 정수를 반환하므로 변환이 필요하지 않습니다.
int nShoppingCart = cmdInsert.ExecuteNonQuery();
오류는 숫자 열에 'NULL'문자열을 삽입 할 때 발생합니다 TotalAmount
.
"'" + DbNull.Value + "'"
에서 발생합니다 'NULL'
(아마, 내가 확인하지 않았지만, 확실히 문자열이 아닌 숫자 값입니다).
NULL 값을 보유 할 수있는 열에 NULL 값을 삽입 할 필요가 없습니다. 단순히 이러한 열을 삽입하지 말고 데이터가 포함 된 열을 삽입하십시오.
그 후 ExecuteScalar를 사용하여 쿼리를 실행합니다. SCOPE-IDENTITY()
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다