Solved

How do I convert an sql parameter value into datetime?

Posted on 2009-05-08
11
501 Views
Last Modified: 2013-12-17
I have written the attached C# code for my web site using visual studio 2008. I have created a dataset for my reportviewer control that is getting filled from the results of a stored procedure. The results from that stored procedure are of the smalldatetime value. Everytime I run my web site I get the error: "Cannot convert string to datetime" on the line:

da.Fill(thisDataSet)

I guess I need to do a conversion somewhere? I also want to show my smalldatetime data as just the date without a time value attached, is this possible?

Thanks.
ReportViewer1.Visible = true;

        SqlConnection thisConnection = new SqlConnection(thisConnectionString);

        SqlCommand cmd = new SqlCommand("CrucialDates.proc_ShowCrucialByDate", thisConnection);

        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.AddWithValue("@DueDateFrom", tbCrucialSearchDateFrom.Text);

        cmd.Parameters.AddWithValue("@DueDateTo".ToString(), tbCrucialSearchDateTo.Text);

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        System.Data.DataSet thisDataSet = new System.Data.DataSet();

        da.Fill(thisDataSet);
 

        ReportDataSource datasource = new ReportDataSource("CrucialDatesDataSet_proc_ShowCrucialByDate", thisDataSet.Tables[0]);
 

        ReportViewer1.LocalReport.DataSources.Clear();

        ReportViewer1.LocalReport.DataSources.Add(datasource);

        if (thisDataSet.Tables[0].Rows.Count == 0)

        {

            lblMessage.Text = "No Crucial Dates are due!";

        }
 

        ReportViewer1.LocalReport.Refresh();

Open in new window

0
Comment
Question by:Shepwedd
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 8

Expert Comment

by:arhame
ID: 24335882
Hmm, this is the syntax I use when doing a SQL datetime insert:

insertCommand.Parameters.Add("LaborDate", Data.SqlDbType.DateTime).Value = tbDateWorked.Text
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 24335968
Have you tried
        cmd.Parameters.AddWithValue("@DueDateFrom", Convert.ToDateTime(tbCrucialSearchDateFrom.Text));

        cmd.Parameters.AddWithValue("@DueDateTo".ToString(), Convert.ToDateTime(tbCrucialSearchDateTo.Text));

Open in new window

0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 24336069
Hi Shepwedd;

Try changing the two lines in the code snippet.

Fernando
cmd.Parameters.AddWithValue("@DueDateFrom", DateTime.Parse(tbCrucialSearchDateFrom.Text));

cmd.Parameters.AddWithValue("@DueDateTo", DateTime.Parse(tbCrucialSearchDateTo.Text));

Open in new window

0
 

Author Comment

by:Shepwedd
ID: 24336098
kaufmed,

That worked, great!

I also want to show my smalldatetime data as just the date without a time value attached, is this possible?

Thanks.
0
 

Author Comment

by:Shepwedd
ID: 24336118
That worked as well Fernando.

Can you help me with my other question stated above?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 24336136
DateTime objects have an overloaded ToString() method that takes a formatting parameter:
DateTime d = DateTime.Now;
 

d.ToString("MM/dd/yyyy");   // Note 'M' as opposed to 'm'; 'm' is for minutes

Open in new window

0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 24336256
Hi Shepwedd;

I am not a ASP .Net programmer and am not sure about ReportViewer for the web. Let me look at the documentation and will post in a little while.

Fernando
0
 

Author Comment

by:Shepwedd
ID: 24336275
Thanks Fernando.
0
 

Author Comment

by:Shepwedd
ID: 24336287
kaufmed,

How do I use your suggested code so my reportviewer control shows the correctly formatted date?
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 24336745
Hi Shepwedd;

Try the following:

When you design your MicrosoftReportViewer and have it all defined, Page, Group and Columns click on the Row with the Date Field, the text box should start with =Field!... Once the text box is selected right click on it and select Properties from the context menu. When the TextBox Properties windows open select the Format tab at the top of the page. To the right of the "Format code:" textbox their is a button with the text "...", click on it. When the Choose Format window opens select date in the left list box and in the right listbox select 5/8/2009. Then click on "OK" button, Then click on "OK" button again in the other window and that should do it.

Fernando
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 24337354
Hi Shepwedd;

Try the following:

When you design your MicrosoftReportViewer and have it all defined, Page, Group and Columns click on the Row with the Date Field, the text box should start with =Field!... Once the text box is selected right click on it and select Properties from the context menu. When the TextBox Properties windows open select the Format tab at the top of the page. To the right of the "Format code:" textbox their is a button with the text "...", click on it. When the Choose Format window opens select date in the left list box and in the right listbox select 5/8/2009. Then click on "OK" button, Then click on "OK" button again in the other window and that should do it.

Fernando
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now