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:
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 GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
e.NewValues["Login_Time"] = DateTime.Now;
}
|||protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
e.Values["Login_Time"] = DateTime.Now;
}
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:
and put this in Page_Load event:<Insert Parameters><asp:Parameter Type="DateTime" Name="Login_Time" /></Insert Parameters>
|||<asp:ButtonID="login1"runat="server"Text="Login"SqlDataSource1.InsertParameters["Login_Time"].DefaultValue = DateTime.Now.ToString();
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:
And one question: Why are you opening connection in Page_Load event?: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");
}
If you're using SqlDataSource this is unnecessarily.|||SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=engineering; Integrated Security=True");
conn.Open();
GridView1.DataBind();
conn.Close();
THANK YOU SO MUCH!!!!!
No comments:
Post a Comment