2013-07-09 180 views
0

我想用ComboBox將記錄插入數據庫。組合框連接到其他的桌子,這是錯誤:」將數據類型nvarchar轉換爲數字時出錯。

Error converting data type nvarchar to numeric.

private void InsertReceipt() 
{ 
    SqlCommand cmd = new SqlCommand(); 
    cmd.Connection = cn; 
    cmd.CommandType = CommandType.Text; 
    cmd.CommandText = "INSERT INTO Ticket(CustomerID, Date, Store, Amount, NoStub)" + 
         "VALUES (@CustomerID, @Date, @Store, @Amount, @NoStub) "; 
    cmd.Parameters.AddWithValue("@CustomerID", cboName.SelectedValue); 
    cmd.Parameters.AddWithValue("@Date", dtpDate.Value.Date.ToString()); 
    cmd.Parameters.AddWithValue("@Store", txtStore.Text); 
    cmd.Parameters.AddWithValue("@Amount", txtAmount.Text); 
    cmd.Parameters.AddWithValue("@NoStub", txtStub.Text); 
    cmd.ExecuteNonQuery(); 
} 

void GetRecords2() 
{ 
    SqlCommand cmd = new SqlCommand(); 
    cmd.Connection = cn; 
    cmd.CommandText = "SELECT CustomerID, firstname + ', ' + lastname AS Name FROM Customer"; 

    SqlDataAdapter adp = new SqlDataAdapter(cmd); 
    DataSet ds = new DataSet(); 
    adp.Fill(ds, "Customer"); 

    cboName.DataSource = ds; 
    cboName.DisplayMember = "Customer.Name"; 
    cboName.ValueMember = "Customer.CustomerID"; 
} 
+2

使用Int.ParseInt(txtAmount.Text)將其另存爲數字或Float.ParseFloat。你沒有給出一個模式,所以我猜測。 –

回答

2

當你調用AddWithValue確保您傳遞的數據類型的列類型相匹配。這裏是一個可能的候選人:

cmd.Parameters.AddWithValue("@Amount", txtAmount.Text); 

在此行要傳遞的文本字符串的東西,顯然想要一個數值(量)。你應該先解析txtAmount.Text成十進制,然後傳遞值:

decimal amount = decimal.Parse(txtAmount.Text); 
cmd.Parameters.AddWithValue("@Amount", amount); 

有了這個代碼,你仍可能得到一個異常,如果在txtAmount.Text字符串不能被解析到一個小數,但至少你會知道哪個值導致了問題。您可以/應該對其他值也做相同的操作,以確保它們與列類型匹配。

0

嘗試string.isNullorEmpty(txtAmount.text);

-1
private void button1_Click(object sender, EventArgs e) 
     { 
      string sql; 
      sql = "insert into slab (date,sober_visor,tesh,shift,group,heat_no,st_grade,thick,width,length,location,pcs,remarkes,slab_no) values (@date,@sober_vsor,@tesh,@shift,@group,@heat_no,@st_grade,@thick,@width,@length,@loction,@pcs,@slab_no);select scope_identity()"; 

      SqlCommand cmd = new SqlCommand(sql, con); 
      cmd.Parameters.AddWithValue("@date", txt_date.Text); 
      cmd.Parameters.AddWithValue("@sober_visor", com_sober_visor.ToString()); 
      cmd.Parameters.AddWithValue("@shift", txt_shift.Text); 
      cmd.Parameters.AddWithValue("@heat_no", txt_heat_no.Text); 
      cmd.Parameters.AddWithValue("@thick", txt_shift.Text); 
      cmd.Parameters.AddWithValue("@width", txt_heat_no.Text); 
      cmd.Parameters.AddWithValue("@length", txt_length.Text); 
      cmd.Parameters.AddWithValue("@pcs", txt_pcs.Text); 
      cmd.Parameters.AddWithValue("@st_grade", txt_st_gread.Text); 
      cmd.Parameters.AddWithValue("@location", txt_loction.Text); 
      cmd.Parameters.AddWithValue("@slab_no", txt_slab_no.Text); 
      con.Open(); 
      cmd.ExecuteNonQuery(); 
      txt_heat_no.Text = cmd.ExecuteScalar().ToString(); 

      con.Close(); 
      MessageBox.Show("تمت عملية الإضافة"); 
     } 
    } 
} 
+1

您發佈了完全不同的代碼,沒有任何解釋。 – mmushtaq

相關問題