1
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.

4
  • 1
    what problem are you facing to retrieve that? Commented Aug 26, 2013 at 22:39
  • What do you get when you run that code? That should output something in the box. Commented Aug 26, 2013 at 22:39
  • Are you getting an error? Retrieving a date from sql server is pretty straight forward and shouldn't be a problem Commented Aug 26, 2013 at 22:40
  • There's no any error when I run this program, it just didn't pop out the date into my textbox. Commented Aug 26, 2013 at 23:03

2 Answers 2

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.

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.

Then take a look at the Standard Date and Time format strings on MSDN here: 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.

Sign up to request clarification or add additional context in comments.

Comments

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.

  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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.