2011-09-03 61 views
0

插入日期時間爲接取數據庫,這可能是一個非常普遍的老問題,但我仍然有過相當多的努力,最後我在這裏張貼:通過C#和Asp.net

我已經在Access數據庫中插入DateTime,但我遇到問題。 我的C#代碼:

protected void Button5_Click(object sender, EventArgs e) 
    { 
     Panel1.Visible = true; 
     Panel2.Visible = false; 


     String eventname = 
      TextBox6.Text; 
     String description = TextBox7.Text; 

      String coord = TextBox8.Text; 

      String venue = TextBox9.Text; 

      String time = TextBox10.Text; 

      TextBox6.Text = ""; 
      TextBox7.Text = ""; 
      TextBox8.Text = ""; 
      TextBox9.Text = ""; 
      TextBox10.Text = "";  


     AccessDataSource1.InsertParameters["eventname1"].DefaultValue 
      = eventname; 
     AccessDataSource1.InsertParameters["description1"].DefaultValue 
      = description; 
     AccessDataSource1.InsertParameters["coordinator1"].DefaultValue 
      = coord; 
     AccessDataSource1.InsertParameters["venue1"].DefaultValue 
      = venue; 
     AccessDataSource1.InsertParameters["time1"].DefaultValue 
      = time; 
     AccessDataSource1.InsertParameters["time1"].DefaultValue 
      = time; 
     AccessDataSource1.Insert(); 

     DataList1.EditItemIndex = -1; 
     DataList1.DataBind(); 
    } 

以及相應的Asp.Net腳本:

<asp:AccessDataSource ID="AccessDataSource1" runat="server" 
     DataFile="~/events.accdb" SelectCommand="SELECT * FROM [upcoming]" 

     InsertCommand="INSERT INTO [upcoming] ([eventname], [description],[coordinator], [venue],[time]) 
     VALUES (@eventname1, @description1,@coordinatior1, @venue1, @time1)" > 


     <InsertParameters> 
      <asp:Parameter Name="eventname1" Type="String" /> 
      <asp:Parameter Name="description1" Type="String" /> 
      <asp:Parameter Name="coordinator1" Type="String" /> 
      <asp:Parameter Name="venue1" Type="String" /> 
      <asp:Parameter Name="time1" Type="DateTime" /> 

     </InsertParameters> 

    </asp:AccessDataSource> 
<asp:Panel ID="Panel2" runat="server"> 
    <table> 
      <tr><td><asp:Label ID="Label6" runat="server" Text="Name:"></asp:Label></td> 
      <td class="style1"> <asp:TextBox ID="TextBox6" runat="server" 
        Height="24px" Width="548px"></asp:TextBox></td></tr> 

      <tr><td><asp:Label ID="Label7" runat="server" Text="Description:"></asp:Label></td> 
      <td class="style1"> <asp:TextBox ID="TextBox7" runat="server" 
        Height="155px" TextMode="MultiLine" 
        Width="555px"></asp:TextBox></td></tr> 

      <tr><td><asp:Label ID="Label8" runat="server" Text="Event Coordinators:"></asp:Label></td> 
      <td class="style1"> 
       <asp:TextBox ID="TextBox8" runat="server" 
       Width="548px"></asp:TextBox></td></tr> 

      <tr><td> <asp:Label ID="Label9" runat="server" Text="Venue:"></asp:Label></td> 
      <td class="style1"> 
       <asp:TextBox ID="TextBox9" runat="server" 
       Width="546px"></asp:TextBox></td></tr> 

      <tr><td><asp:Label ID="Label10" runat="server" Text="Date &Time:"></asp:Label></td> 
      <td class="style1"> 
       <asp:TextBox ID="TextBox10" runat="server" 
       Height="23px" Width="543px"></asp:TextBox></td> 
       </tr> 

      <tr><td> 
       <asp:Button ID="Button5" runat="server" Text="Insert" 
        onclick="Button5_Click" /></td> 
      <td class="style1"> <asp:Button ID="Button6" runat="server" CommandName="Cancel" 
        Text="Cancel" onclick="Button6_Click" /></td></tr> 
      <table/> 
</asp:Panel> 

請幫我通過指定的機制,插入日期時間。

+0

「我無法這樣做。」 - 是不是一個真正的問題... –

+0

你會得到任何錯誤? – Waqas

+0

@Waqas在從c#代碼文件傳遞到aspx文件時,time1參數值被轉換爲字符串,所以問題是,如何將它再次轉換回datetime類型? –

回答

1

你要使用的方法的TryParse在運行時處理可能的例外。

 string time = this.TextBox10.Text; 
     DateTime dt = new DateTime(); 
     if (DateTime.TryParse("12/12/2011", out dt)) 
     { 
      // your code here 
     } 

問候

+0

非常感謝!我已經解決了這個難題。 –

+0

你好Anant Gupta,你好 –

0

根據你的評論,你正在尋找一種方式來字符串轉換爲DateTime類型:

String time = TextBox10.Text; 
DateTime dt = Convert.ToDateTime(time);