Sunday, February 26, 2012

error:the string was not recognized as a valid DateTime.

hi all, i'm trying to insert the time/date a button was clicked on a gridview and it generates an error:the string was not recognized as a valid DateTime.There is an unknown word starting at index 0 i have changed the culture to en-US but it still doesn't work. i actually created the date/time column after some data had been entered into the table so the column allows nulls. this is my code:

InsertCommand="INSERT INTO test101(Surname,Names,Registration,Login Time)VALUES (@.Surname, @.Names, @.Registration,@.Login_Time)"

<Insert Parameters><asp:Parameter DefaultValue= DateTime.Now Type=DateTime Name="Login_Time" /></Insert Parameters
any suggestions?

When do you recieve that error? You mentioned button on a GridView, so I suppose that you're trying to update some values and, if that's the case, use this code:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
e.NewValues["Login_Time"] = DateTime.Now;
}

If, on the other hand, you're trying to insert some values in database, use this code (this is example for inserting values with DetailsView ):

protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
e.Values["Login_Time"] = DateTime.Now;
}

|||

sorry, this message was supposed to be:

hi all, i'm trying to insert the time/date at the time a user clicks the submit button but it generates an error:the string was not recognized as a valid DateTime.There is an unknown word starting at index 0 i have changed the culture to en-US but it still doesn't work. this is my code:

InsertCommand="INSERT INTO test101(Surname,Names,Registration,Login Time)VALUES (@.Surname, @.Names, @.Registration,@.Login_Time)"

<Insert Parameters><asp:Parameter DefaultValue= DateTime.Now Type=DateTime Name="Login_Time" /></Insert Parameters
any suggestions?

|||

Where is that submit button (in DetailsView or in FormView)?

|||

it's on a login page. so i want the time the user logged on to be saved on a database

|||

Can you post your whole code?

|||

One way to achieve your goal is to change your InsertParameter into this:

<Insert Parameters><asp:Parameter Type="DateTime" Name="Login_Time" /></Insert Parameters>

and put this in Page_Load event:

SqlDataSource1.InsertParameters["Login_Time"].DefaultValue = DateTime.Now.ToString();

|||<asp:ButtonID="login1"runat="server"Text="Login"

Height="31px"OnClick="login1_Click"Width="65px"BackColor="#E0E0E0"Font-Size="Medium"/>

<asp:ButtonID="Reset1"runat="server"BackColor="#E0E0E0"Font-Size="Medium"

Height="32px"OnClick="Reset1_Click"Text="Reset"Width="63px"/>

<br/>

<br/>

<asp:SqlDataSourceID="SqlDataSource1"runat="server"

ConnectionString="<%$ ConnectionStrings:engineeringConnectionString %>"

InsertCommand="INSERT INTO test101(Surname,Names,Registration,[Course code],[Login Time]) VALUES (@.Surname,@.Names,@.Registration,@.Course_code,@.Login_Time)">

<InsertParameters>

<asp:ControlParameterControlID="TextBox1"DefaultValue="Textbox1.Text"Name="Surname"

PropertyName="Text"/>

<asp:ControlParameterControlID="TextBox2"DefaultValue="TextBox2.Text"Name="Names"

PropertyName="Text"/>

<asp:ControlParameterControlID="TextBox3"DefaultValue="TextBox3.Text"Name="Registration"

PropertyName="Text"/>

<asp:ControlParameterControlID="TextBox4"DefaultValue="TextBox4.Text"Name="Course_code"

PropertyName="Text"/>

<asp:Parameter Name="Login_Time" type=DateTime/>

</InsertParameters>

</asp:SqlDataSource>

<asp:GridViewID="GridView1"

runat="server"AutoGenerateColumns="False"AutoGenerateDeleteButton="True"DataKeyNames="ID"

AutoGenerateEditButton="True"AllowSorting="True"BackColor="LightGoldenrodYellow"BorderColor="Tan"BorderWidth="1px"CellPadding="2"ForeColor="Black"GridLines="None"PageSize="20"

Height="374px"EmptyDataText="null"DataSourceID=SqlDataSource1Visible="False"AllowPaging="True">

<Columns>

<asp:BoundFieldDataField="ID"HeaderText="ID"InsertVisible="False"ReadOnly="True"

SortExpression="ID"/>

<asp:BoundFieldDataField="Surname"HeaderText="Surname"SortExpression="Surname"/>

<asp:BoundFieldDataField="Names"HeaderText="Names"SortExpression="Names"/>

<asp:BoundFieldDataField="Registration"HeaderText="Registration"SortExpression="Registration"/>

<asp:BoundFieldDataField="Course code"HeaderText="Course code"SortExpression="Course code"/>

<asp:BoundFieldDataField="Grade"HeaderText="Grade"SortExpression="Grade"/>

<asp:BoundFieldDataField="login Time"HeaderText="Login Time"SortExpression="login Time"/>

</Columns>

</asp:GridView>

and this is the code behind:

protectedvoid Page_Load(object sender,EventArgs e)

{

SqlConnection conn =newSqlConnection("Data Source=(local);Initial Catalog=engineering; Integrated Security=True");

conn.Open();

GridView1.DataBind();

conn.Close();

}

publicvoid login1_Click(object sender,EventArgs e)

{

SqlDataSource1.Insert();

if (TextBox1.Text !="" && TextBox2.Text !="" && TextBox3.Text !="" && TextBox4.Text !="")

{

Response.Redirect("test1.aspx");

}

|||

Just add SqlDataSource1.InsertParameters["Login_Time"].DefaultValue = DateTime.Now.ToString(); in login1_Click event like this:

public void login1_Click(object sender, EventArgs e)
{
SqlDataSource1.InsertParameters["Login_Time"].DefaultValue = DateTime.Now.ToString();
SqlDataSource1.Insert();
if (TextBox1.Text != "" && TextBox2.Text != "" && TextBox3.Text != "" && TextBox4.Text !="")
{
Response.Redirect("test1.aspx");
}

And one question: Why are you opening connection in Page_Load event?:

SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=engineering; Integrated Security=True");
conn.Open();
GridView1.DataBind();
conn.Close();

If you're using SqlDataSource this is unnecessarily.

|||

THANK YOU SO MUCH!!!!!

No comments:

Post a Comment