2011-06-20 22 views
1

私はC#を学び、Visual Studio 2010を使用して給与計算プログラムを作成しています。私は "暗黙のうちに型 'int'を 'short'に変換できないというエラーが発生します。C#エラー:暗黙的に型 'int'を 'short'に変換できません

私のコードは次のとおりです。私のデータベースは、U_Tax_yearはSMALLINTと定義されているMicrosoft SQL Serverでの

private void btn_add_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       // Get service instance 
       var employerPeriodService = Program.Kernel.Get<IEmployerPeriodService>(); 

       // Get new code 
       var newCode = employerPeriodService.GenerateSAPCode(); 

       // Create object 
       var employerPeriodAdd = 
        new EmployerPeriod 
        { 
         Code = newCode, 
         Name = newCode, 
         U_Tax_year = int.Parse(cb_tax_year.Text), 
         //U_Day_hrs = cb_number_hours.Text, 
         //U_Week_days = cb_number_days_week.Text, 
         //U_Week_hrs = txt_number_hours_week.Text, 
         //U_Month_days = cb_number_days_month.Text, 
         //U_Month_hrs = txt_number_hours_month.Text, 
         //U_Fortnight_days = cb_number_days_fortnight.Text, 
         //U_Fortnight_hrs = txt_number_hours_fortnight.Text, 
         //U_Weeks_in_month = cb_avg_weeks_month.Text, 
         //U_No_of_Fortnights = cb_avg_fortnights_month.Text, 
         U_Starting_period = Convert.ToDateTime(dateTimePicker1.Text), 
         U_Ending_period = Convert.ToDateTime(dateTimePicker2.Text) 

         //U_Comments = txt_comments.Text 
        }; 

       // Save record 
       employerPeriodService.AddEmployerPeriod(employerPeriodAdd); 

       MessageBox.Show("Employer Payroll Period Added Successfully. Intake Ref: " + newCode.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
      } 
      catch (RulesException ex) 
      { 
       MessageBox.Show(ex.GetErrorMessages(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
      } 
     } 

。このエラーを解決するためには何が必要ですか。私はコンボボックス 'cb_tax_year.Text'からintに変換し、どのようにこれを達成しますか?

助けてください。これに

U_Tax_year = int.Parse(cb_tax_year.Text), 

+0

あなたはshort.parse intの代わりに試しましたか? –

答えて

8

私はそれがこれを変えるのと同じくらい簡単だ疑い

基本的に
U_Tax_year = short.Parse(cb_tax_year.Text), 

あなたがU_Tax_year財産の種類を確認し、適切なテキストを解析を確認する必要がありますように=記号のRHSの結果は、プロパティタイプと割り当て互換性があります。

+0

ありがとうございます。私は、intがint型かlong int型かどうかを考えていました。解決済み。 –

3

使用short.Parseではなくint.Parse

3

のSQLServerのsmallintは、C#のshortのと同じです。だから、の代わりにshort.Parse()を試してください

関連する問題