如何在页脚添加按钮单击时将日期时间存储在数据库中 [英] How to Store date time in Database when footer Add button Click
问题描述
嗨frnds,
我正在使用Asp.net,C#,SqlServer 2005.
在网页上,我有Gridview从页脚文本框输入记录。
我想在页脚添加按钮点击时保存记录添加日期和时间数据库。
我也想保存添加记录的用户名。
我们有这个项目的登录页面.....所以我必须保存用户名也添加记录时。
在数据库中我有两个字段
1)Record_EnteredDate
2)Record_EnteredBy。
这是我从行命令添加新记录的代码
Hi frnds,
am working on Asp.net,C#,SqlServer 2005.
On webpage I have Gridview to enter the records from Footer Textboxes.
I want to Save record added Date and Time in Database when Footer add button click.
and also I want to Save Username who added the record.
We have login page for this proj.....so I must save Username also when adding record.
In database I have Two fields
1)Record_EnteredDate
2)Record_EnteredBy.
and This is my code for adding New Record from Row Command
protected void GVRecordsEntry_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox TxtStudentName = (TextBox)GVRecordsEntry.FooterRow.FindControl("txtftrStudentName");
TextBox TxtRollNo = (TextBox)GVRecordsEntry.FooterRow.FindControl("txtftrRollNo");
TextBox TxtFatherName = (TextBox)GVRecordsEntry.FooterRow.FindControl("txtftrFatherName");
TextBox TxtLocation = (TextBox)GVRecordsEntry.FooterRow.FindControl("txtftrLocation");
// Add Record Entry Date time
// Add Record Entered UserName
con.Open();
SqlCommand cmd = new SqlCommand("INSERT into StudentRecs (StudentName,RollNo,FatherName,Location) VALUES ('" + TxtStudentName .Text + "','" + TxtRollNo.Text + "','" + TxtFatherName.Text + "','" + TxtLocation.Text + "')", con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
LoadInventoryData();
}
}
}
只是我想添加记录输入日期和时间....
和Recored由UserName输入。
请帮帮我。
先谢谢。
Just I want to add Record Entered Date and Time....
and Recored entered by UserName.
Please help me.
Thanks in Advance.
推荐答案
在你的桌子上添加两列StudentRecs (UserID,EntryDate)
在登录时将UserID存储在会话中Session [UserID] = UserID
然后修改你这样的查询
Add two columns to your table StudentRecs (UserID,EntryDate)
Store the UserID in session at login time Session["UserID"]=UserID
then modify you query like this
SqlCommand cmd = new SqlCommand("INSERT into StudentRecs (StudentName,RollNo,FatherName,Location,UserID,EntryDate) VALUES ('" + TxtStudentName .Text + "','" + TxtRollNo.Text + "','" + TxtFatherName.Text + "','" + TxtLocation.Text + "','"+Session[UserID].ToString()+"',getdate())", con);
希望这会解决你的问题
hope this will fix your problem
这篇关于如何在页脚添加按钮单击时将日期时间存储在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!