2017-03-22 12 views
0

フォームにあるすべてのデータをEquipmentというデータベースに挿入したいとします。 しかし、保存ボタンをクリックすると、「パラメータ値を文字列からDateTimeに変換できませんでした」というエラーが表示されます。修正するには文字列のパラメータ値をasp.netのDateTimeに変換できませんでしたか?

日付型の特定の列のデータ型を設定しました。私は、テキストボックスにカレンダーを置くので、テキストボックスをクリックすると、カレンダーが表示されます。ユーザーは日付を選択できます。 javascriptを使用して作成されているカレンダー。

誰でもこの問題の解決方法を知っていますか?

The form and the calendar pop up

ボタンを救うためのコード:

string connectionstring = "Data Source=5CG50749V3\\SQLEXPRESS;Initial Catalog=test;Integrated Security=True"; 
    using (SqlConnection connection = new SqlConnection(connectionstring)) 
    { 

     string sql = @"INSERT Into [Equipment]([OwnerID],[SubjecttoCal],[Model],[Option],[EquipmentID],[SerialNumber],[Description],[Location], 
          [DueDate],[EquipmentWithdraworRemarks],[NCRorOOTHistory],[LastOOTissuanceDate], 
          [AvailableinSapphire],[ResponsiblePerson],[CalibrationOption],[CalibrationSourceorLab], 
          [YearofManufacturing],[ManufacturerorVendor],[CalibrationCost],[AssetNo],[CalibrationTAT],[SendInDate],[Status]) 
          "; 

     using (SqlCommand command = new SqlCommand(sql, connection)) 
     { 
      command.Parameters.Add("@OwnerID", SqlDbType.NVarChar).Value = TextBox1.Text; 
      command.Parameters.Add("@SubjecttoCal", SqlDbType.NVarChar).Value = TextBox2.Text; 
      command.Parameters.Add("@Model", SqlDbType.NVarChar).Value = TextBox3.Text; 
      command.Parameters.Add("@Option", SqlDbType.NVarChar).Value = TextBox4.Text; 
      command.Parameters.Add("@EquipmentID", SqlDbType.NVarChar).Value = TextBox5.Text; 
      command.Parameters.Add("@SerialNumber", SqlDbType.NVarChar).Value = TextBox6.Text; 
      command.Parameters.Add("@Description", SqlDbType.NVarChar).Value = TextBox7.Text; 
      command.Parameters.Add("@Location", SqlDbType.NVarChar).Value = DropDownList1.Text; 
      command.Parameters.Add("@DueDate", SqlDbType.DateTime).Value = TextBox8.Text; 
      command.Parameters.Add("@EquipmentWithdraworRemarks", SqlDbType.NVarChar).Value = TextBox10.Text; 
      command.Parameters.Add("@NCRorOOTHistory", SqlDbType.NVarChar).Value = TextBox11.Text; 
      command.Parameters.Add("@LastOOTissuanceDate", SqlDbType.DateTime).Value = TextBox12.Text; 

      command.Parameters.Add("@AvailableinSapphire", SqlDbType.NVarChar).Value = TextBox13.Text; 
      command.Parameters.Add("@ResponsiblePerson", SqlDbType.NVarChar).Value = TextBox14.Text; 
      command.Parameters.Add("@CalibrationOption", SqlDbType.NVarChar).Value = TextBox15.Text; 
      command.Parameters.Add("@CalibrationSourceorLab", SqlDbType.NVarChar).Value = TextBox16.Text; 

      command.Parameters.Add("@YearofManufacturing", SqlDbType.NVarChar).Value = TextBox17.Text; 
      command.Parameters.Add("@ManufacturerorVendor", SqlDbType.NVarChar).Value = TextBox18.Text; 
      command.Parameters.Add("@CalibrationCost", SqlDbType.NVarChar).Value = TextBox19.Text; 
      command.Parameters.Add("@AssetNo", SqlDbType.NVarChar).Value = TextBox20.Text; 

      command.Parameters.Add("@CalibrationTAT", SqlDbType.NVarChar).Value = TextBox21.Text; 
      command.Parameters.Add("@SendInDate", SqlDbType.DateTime).Value = TextBox22.Text; 
      command.Parameters.Add("@Status", SqlDbType.NVarChar).Value = DropDownList2.Text; 

      connection.Open(); 
      command.ExecuteNonQuery(); 
      connection.Close(); 
     } 

     { 
      this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Your data have been saved')", true); 



     } 
    } 

javacriptコード:

<script type="text/javascript"> 
    $(function() { 
     $("[id$=TextBox8]").datepicker({ 
      dateFormat: 'dd/mm/yy', changeYear: true 
     }); 
     $("[id$=TextBox12]").datepicker({ 
      dateFormat: 'dd/mm/yy', changeYear: true 
     }); 
     $("[id$=TextBox17]").datepicker({ 
      dateFormat: 'dd/mm/yy', changeYear: true 
     }); 
     $("[id$=TextBox22]").datepicker({ 
      dateFormat: 'dd/mm/yy', changeYear: true 
     }); 
    }); 
</script> 
+0

これは、Webフォームではなく、MVCのように見えますか?明示的にdatetimeパラメーターに文字列を割り当てます。手前の日付形式を知っている場合は、最初に解析してください。 –

答えて

0

あなたの日付文字列は日時形式に変換する必要があります。例えばのため:

DateTime oDate = DateTime.ParseExact(textboxValue, "MM/dd/yy", null); 

//For eg 
command.Parameters.Add("@DueDate", SqlDbType.DateTime).Value = DateTime.ParseExact(TextBox8.Text, "MM/dd/yy", null); 

ところで、私はあなたの日時ピッカーからTextBox8.Text返り正しい値を仮定しています。

それはあなたのコードを見ていたようそしてちょうど他の日時テキストボックスと同じことを行う

+0

このコードをどこに置くか教えていただけますか?申し訳ありませんが、私はまだasp.netで新しいです。 – Moon

+0

ありがとうございます。やってみます。 – Moon

+0

word textboxValue、TextBox.Textで置き換える必要がありますか? – Moon

0

に動作する場合は、テキストボックスに、クライアント側から「DD/MM/YYYY」の形式で日付を取っています。 したがって、次のスニペットでその形式を考慮して日付を解析する必要があります。あなたはデータ型をmentiondいるwhereever声明 "DateTime.ParseExact(textbox.Text、 "DD/MM/YYYY"、CultureInfo.InvariantCulture)" であなたの "textbox.Text" を交換する必要があなたのコードの場合

DateTime parsedDate = DateTime.ParseExact("22/3/2017", "dd/MM/yyyy", CultureInfo.InvariantCulture); 

日付時刻。

すなわちは

 command.Parameters.Add("@DueDate", SqlDbType.DateTime).Value = DateTime.ParseExact(TextBox8.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture); 

     command.Parameters.Add("@LastOOTissuanceDate", SqlDbType.DateTime).Value = DateTime.ParseExact(TextBox12.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture); 

     command.Parameters.Add("@SendInDate", SqlDbType.DateTime).Value = DateTime.ParseExact(TextBox22.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture); 
+0

ありがとうございます。しかし、cultureinfoのテキストに赤い線が表示されるのはなぜですか? – Moon

+0

は、System.Globalizationを使用して追加していない可能性があります。 –

+0

大丈夫です。しかし、私はそれを実行すると、このエラー{"文字列は有効なDateTimeとして認識されませんでした"}を表示します。どういう意味? @HemantDhake – Moon

関連する問題