Showing posts with label control. Show all posts
Showing posts with label control. Show all posts

Friday, March 30, 2012

Saving the time with the date

I have a calendar control on a page that is saving the date to a SQL Server 2005 table and this works fine; but how do I also save it with the current time? It is always saving as 12:00:00 AM.

Thanks for the help.

Check your calendar control properties because it probably is setup to display/send only date. In this case SQL server add dfault time for date which is 12 AM.
It should have options like date and time, date only, time only.

Thanks

|||

It seems odd you would use a date from a calendar and want the current time but...

DateTime DateOnly = new DateTime(2007, 5, 3);
DateTime RightNow = DateTime.Now;

// add current time to date

DateOnly = DateOnly.AddHours(RightNow.Hour);
DateOnly = DateOnly.AddMinutes(RightNow.Minute);
DateOnly = DateOnly.AddSeconds(RightNow.Second);

|||

This is an intranet application where our CSRs record incoming items and the time stamp needs to be with the record. I solved it by putting System.DateTime.Now() into a Session variable.

Thanks for the responses. Now if I can figure out my "Access is Denied" problem with ajax...

Friday, March 9, 2012

save and retrieve RTF TEXT FROM SQL DATABASE

Hi,
i have a richtextbox control in my application, and want to know how to save and retrieve formatted text in a MS SQL DB.

I found this example in the forum, but I don't know how to use it. Can you please help me solve the problem? Where should I write the code below?

I've just tried it and able to do this successfully.

I basically did this:

load the RTF into the RTB control created a SQL command, simple basic insert statement:



SqlCommand theSQLCommand = new SqlCommand("INSERT INTO [TableName] (Field) VALUES (@.p1)");
SqlParameter theSQLParameter = new SqlParameter("@.p1", SqlDbType.Text);
theSQLParameter.Value = this.theRichTextBox.RTF;
theSQLCommand.Parameters.Add(theSQLParameter);
theSQLCommand.Connection = new SqlConnection(ConnectionString);
theSQLCommand.Connection.Open();
theSQLCommand.ExecuteNonQuery();
theSQLCommand.Connection.Close();

then to retrieve it, I did this, again, specifically for this example



this.theRichTextBox.Text = String.Empty;
SqlCommand theSQLCommand = new SqlCommand("SELECT [Field] FROM [TableName] WHERE [ID] = 1");
theSQLCommand.Connection = new SqlConnection(ConnectionString);
theSQLCommand.Connection.Open();
SqlDataReader theReader = theSQLCommand.ExecuteReader(CommandBehavior.CloseConnection);
while (theReader.Read())
{
this.theRichTextBox.RTF = theReader.GetValue(0).ToString();
}
theSQLCommand.Connection.Close();

And was able to do this fine and got all the formatting etc... correctly.

The two block codes are from a Windows Forms .NET application with a richtextbox control called theRichTextBox. You would just need to create a table with a column of type TEXT and replace the appropriate [Field] and [TableName] values with the table and column as appropriate. Also, (hopefully obviously) you would need to replace ConnectionString with the parameters to connect this to your specific database.