2013-05-05 97 views
0

我得到了一個ComboBox,其中一個表格作爲數據源,ID作爲值成員和作爲顯示成員的名稱。 從ComboBox中選擇一個名稱應該用數據填充6個TextBoxes。填充文本框,數據類型對於布爾操作無效

例外:

The data type is not valid for the boolean operation. [ Data type (if known) = int,Data type (if known) = nvarchar ] 

代碼:

void FillComboBox() 
{ 

//Fill Combo Box 

    SqlCeDataAdapter da = new SqlCeDataAdapter(" SELECT CustomerID, Name FROM Customers", clsMain.con); 
    DataSet ds = new DataSet(); 
    da.Fill(ds); 
    cBox1.DataSource = ds.Tables[0]; 
    cBox1.ValueMember = "CustomerID"; 
    cBox1.DisplayMember = "Name"; 
} 

public frmMain() 
{ 
    InitializeComponent(); 
} 

private void frmMain_Load(object sender, EventArgs e) 
{ 
    clsMain.con.ConnectionString = @"Data Source=|DataDirectory|\Database\Sales.sdf"; 
    clsMain.con.Open(); 

    FillComboBox(); 
} 

private void btnSave_Click(object sender, EventArgs e) 
{ 

//Save Button 

    SqlCeCommand cmd = new SqlCeCommand(); 
    cmd.CommandType = CommandType.Text; 
    cmd.CommandText = " INSERT INTO Customers (Name, Phone1, Phone2, Address, Notes) VALUES (@Name, @Phone1, @Phone2, @Address, @Notes) "; 
    cmd.Connection = clsMain.con; 

    cmd.Parameters.AddWithValue("@Name", txt2.Text.Trim()); 
    if (txt3.Text != "") 
    { 
     cmd.Parameters.AddWithValue("@Phone1", Convert.ToInt32(txt3.Text)); 
    } 
    else 
    { 
     cmd.Parameters.AddWithValue("@Phone1", Convert.DBNull); 
    } 
    if (txt4.Text != "") 
    { 
     cmd.Parameters.AddWithValue("@Phone2", Convert.ToInt32(txt4.Text)); 
    } 
    else 
    { 
     cmd.Parameters.AddWithValue("@Phone2", Convert.DBNull); 
    } 
    cmd.Parameters.AddWithValue("@Address", txt5.Text.Trim()); 
    cmd.Parameters.AddWithValue("@Notes", txt6.Text.Trim()); 



    cmd.ExecuteNonQuery(); 
    MessageBox.Show("Data stored."); 
} 

private void cBox1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (cBox1.SelectedIndex >= 0) 
    { 

     String Code = "SELECT * FROM Customers WHERE CustomerID=" + cBox1.SelectedValue.ToString(); 
     SqlCeDataAdapter da = new SqlCeDataAdapter(Code, clsMain.con); 
     DataSet ds = new DataSet(); 
     da.Fill(ds); 


     txt1.Text = ds.Tables[0].Rows[0]["CustomerID"].ToString(); 
     txt2.Text = ds.Tables[0].Rows[0]["Name"].ToString(); 
     txt3.Text = ds.Tables[0].Rows[0]["Phone1"].ToString(); 
     txt4.Text = ds.Tables[0].Rows[0]["Phone2"].ToString(); 
     txt5.Text = ds.Tables[0].Rows[0]["Address"].ToString(); 
     txt6.Text = ds.Tables[0].Rows[0]["Notes"].ToString(); 
    } 
} 

表詳細信息:

enter image description here

+1

'CustomerID'是一個int,和你傳遞一個字符串。我沒有使用過SQL CE,但是你可以試試這個:'「SELECT * FROM Customers WHERE CustomerID =」+ cBox1.SelectedValue'(即,擺脫SQL查詢中的單引號)。 – Tim 2013-05-05 04:44:21

+0

謝謝,但現在我得到了這個例外。 '解析查詢時出錯。 [令牌行號= 1,令牌行偏移量= 53,令牌出錯=數據]' – Mody 2013-05-05 04:55:20

+0

請發佈您使用的查詢字符串。此外,在調試器中進入代碼,並在構造查詢時查看「cBox1.SelectedValue」的值。 – Tim 2013-05-05 05:21:28

回答

1

要跟進我的評論(有,我從來沒有合作過的警告SQL CE,但我很確定(99.9%)這是正確的)。

在您的SQL查詢字符串中,您用單引號(')包圍了客戶ID。您在SQL中使用單引號(至少爲「nomral」SQL)來表示字符(char \ varchar等)和日期值。您傳遞不帶單引號的數值。

你沒有說明哪一行給你例外,但是根據表結構和你在做什麼(順便提一句,你在問題中給出了很好的細節),似乎錯誤信息告訴你你試圖將一個int與一個varchar進行比較,這是不允許的(因爲它們是不同的數據類型,如錯誤所示)。

要解決此問題,只需刪除從價值單引號中的WHERE子句和構建您的查詢,如下所示:

String Code = "SELECT * FROM Customers WHERE CustomerID=" + cBox1.SelectedValue; 
+0

非常感謝,這工作得很好,我不知道第二個例外。 我收到這條線上的錯誤'da.Fill(ds);' 我不確定查詢字符串是否意味着連接字符串? – Mody 2013-05-05 05:43:30

+0

不客氣。通過查詢字符串我的意思是你傳遞給適配器的SQL查詢。當你打電話給'Fill'時,你還會得到例外嗎? – Tim 2013-05-05 05:46:09

+0

哦對不起右邊你給我的一個''String =「SELECT * FROM Customers」where CustomerID =「+ cBox1.SelectedValue;' 而且我仍然得到異常。 – Mody 2013-05-05 06:12:01

0

謝謝你,它爲我工作。 我在comboBox2有一些客戶的名字。根據所選名稱textbox將返回CustomerID和分配日期值。 以下是代碼爲:

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     try 
     { 
      if(comboBox2.SelectedIndex>0) 
      { 
       con = new SqlCeConnection(s); 
       con.Open(); 
       string code = "select CustomerID,Date_of_Allocation from lockerdetails  
       where CustomerName='" + comboBox2.SelectedValue.ToString() + "'"; 

       SqlCeDataAdapter da = new SqlCeDataAdapter(code, con); 
       SqlCeCommandBuilder cmd = new SqlCeCommandBuilder(da); 
       DataSet ds = new DataSet(); 
       da.Fill(ds); 
       textBox1.Text = ds.Tables[0].Rows[0]["CustomerID"].ToString(); 
       textBox5.Text = ds.Tables[0].Rows[0]["Date_of_Allocation"].ToString(); 
      } 
     } 
     catch(SystemException se) 
     { 
      MessageBox.Show(se.Message); 
     }