如何在asp.net中从数据库中检索日期值到文本框

时间:2022-12-06 22:10:03
protected void Page_Load(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MAGConnString"].ConnectionString);
    SqlCommand com = new SqlCommand("SELECT * from MAGMember",conn);
    SqlDataAdapter da = new SqlDataAdapter(com);
    DataSet ds = new DataSet();
    da.Fill(ds, "MAGMember");
    txtFirstName.Text = ds.Tables["MAGMember"].Rows[0]["Firstname"].ToString();
    txtLastName.Text = ds.Tables["MAGMember"].Rows[0]["Lastname"].ToString();
    txtGender.Text = ds.Tables["MAGMember"].Rows[0]["Gender"].ToString();
    txtDOB.Text = ds.Tables["MAGMember"].Rows[0]["DOB"].ToString();
    txtPassword.Text = ds.Tables["MAGMember"].Rows[0]["Password"].ToString();
    txtEmail.Text = ds.Tables["MAGMember"].Rows[0]["Email"].ToString();
}

This is code to retrieve the data from database to show in textbox, others value from database i can retrieve it just except the date i can't retrieve since i declare the dateofbirth as date in sqlserver.

这是从数据库检索数据以显示在文本框中的代码,其他值来自数据库我可以检索它除了我无法检索的日期,因为我在sqlserver中声明dateofbirth为日期。

2 个解决方案

#1


1  

There's a few issues with this. To begin with, you're not closing your connections and potentially leaking resources.

这有一些问题。首先,您不会关闭连接并可能泄漏资源。

Your date issue is because you probably won't be able to directly convert your SQL date to a .NET DateTime.

您的日期问题是因为您可能无法直接将SQL日期转换为.NET DateTime。

Take a look at DateTime.ParseExact, on MSDN here: http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx. This will help you learn how to parse dates.

在MSDN上查看DateTime.ParseExact:http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx。这将帮助您了解如何解析日期。

Then take a look at the Standard Date and Time format strings on MSDN here: http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

然后在MSDN上查看标准日期和时间格式字符串:http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

This will give you hints as to how exactly you will parse your date. Without some example data and an exact error there really isn't much more we can do to help you.

这将为您提供有关您将如何解析日期的提示。如果没有一些示例数据和确切的错误,我们无法帮助您。

#2


0  

If you want to retrieve a single value from data base and show in text box using Razor in Asp.net, then this answer will guide you in detail. Lets for example we have an employee table(ID, Name,Age) and we want to retreive the name of the Employee whose ID=1.

如果您想从数据库中检索单个值并在Asp.net中使用Razor显示在文本框中,那么这个答案将为您提供详细的指导。举例来说,我们有一个员工表(ID,名称,年龄),我们想要检索ID = 1的员工的姓名。

  var db = Database.Open("EmployeDataBase");
  var dbCommand = "SELECT * FROM Employee WHERE ID = '1'"; 
  var row = db.QuerySingle(dbCommand);
  var name="null";
        if(row != null) 
        {
             name = row.Name;
        }

Now using the Razor and set the value of the text box, and the text box will show the name

现在使用Razor并设置文本框的值,文本框将显示名称

  <input type="textbox" value=@name  name"/>

#1


1  

There's a few issues with this. To begin with, you're not closing your connections and potentially leaking resources.

这有一些问题。首先,您不会关闭连接并可能泄漏资源。

Your date issue is because you probably won't be able to directly convert your SQL date to a .NET DateTime.

您的日期问题是因为您可能无法直接将SQL日期转换为.NET DateTime。

Take a look at DateTime.ParseExact, on MSDN here: http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx. This will help you learn how to parse dates.

在MSDN上查看DateTime.ParseExact:http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx。这将帮助您了解如何解析日期。

Then take a look at the Standard Date and Time format strings on MSDN here: http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

然后在MSDN上查看标准日期和时间格式字符串:http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

This will give you hints as to how exactly you will parse your date. Without some example data and an exact error there really isn't much more we can do to help you.

这将为您提供有关您将如何解析日期的提示。如果没有一些示例数据和确切的错误,我们无法帮助您。

#2


0  

If you want to retrieve a single value from data base and show in text box using Razor in Asp.net, then this answer will guide you in detail. Lets for example we have an employee table(ID, Name,Age) and we want to retreive the name of the Employee whose ID=1.

如果您想从数据库中检索单个值并在Asp.net中使用Razor显示在文本框中,那么这个答案将为您提供详细的指导。举例来说,我们有一个员工表(ID,名称,年龄),我们想要检索ID = 1的员工的姓名。

  var db = Database.Open("EmployeDataBase");
  var dbCommand = "SELECT * FROM Employee WHERE ID = '1'"; 
  var row = db.QuerySingle(dbCommand);
  var name="null";
        if(row != null) 
        {
             name = row.Name;
        }

Now using the Razor and set the value of the text box, and the text box will show the name

现在使用Razor并设置文本框的值,文本框将显示名称

  <input type="textbox" value=@name  name"/>