2017-06-14 29 views
1

我班有什麼問題嗎?我只想大寫首字母和小寫字母。 我得到一個錯誤消息文本框的第一個字母標題案例

無法從空轉變爲對象

這是我的課:下面

class UpperCaseFirstLetter 
{ 
    private string text; 
    public void SetText(Control control) 
    { 
     text = control.Text; 
     text = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(text.ToLower()); 
    } 

代碼是我使用的類:

newConnection.ConnectionM(); 
SqlCommand cmd = SqlConnectionOLTP.cn.CreateCommand(); 
cmd.CommandText = "Insert into CostCategory(CostCategoryName,Description) values (@costcategoryname,@description)"; 
cmd.Parameters.AddWithValue("@costcategoryname",Format.SetText(textBoxCostName)); 
cmd.Parameters.AddWithValue("@description", textBoxCostDescription.Text); 
cmd.ExecuteNonQuery(); 
SqlConnectionOLTP.cn.Close(); 
MessageBox.Show("Save"); 

回答

3

SetText返回void但在cmd.Parameters.AddWithValue中,您正在使用它,因爲它會返回值。將其更改爲

public string SetText(Control control) 
{ 
    text = control.Text; 
    text = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(text.ToLower()); 
    return text; 
} 
2

這裏要考慮幾件事情:

  • 第一,你不需要類爲該任務,方法就足夠了。事件方法是沒有必要的,你可以做到這一點
  • 你的班級沒有返回文本也沒有設置文本來控制
  • 你沒有實例化你的班級。

爲了實現這一目標,你可以簡單地做到這一點

cmd.Parameters.AddWithValue("@costcategoryname", 
    CultureInfo.CurrentCulture.TextInfo.ToTitleCase(textBoxCostName.Text)); 
相關問題