Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi All,

I tried many links but I am unable to find a solution. Request your expertise for the below queries:

1. I have a listbox where dates from the database are populated. I want only year to be displayed in the listbox. Please advise.

2. This is the current select statement : str = "select partyname from casetab WHERE jdate='" +listJDate.SelectedItem.ToString() + "'";

How do I convert string to datetime in select statement so I can add in the listbox selectIndexChanged event.


Please advise.

--
NG
Posted
Comments
[no name] 18-May-13 14:58pm    
1. Okay so use the year from your DateTime object.
2. Never use string concatentation to construct queries. Use parameterized queries instead. Then the framework will do the converting for you.

Try this
C#
str = "select partyname from casetab WHERE jdate='" +Convert.ToDateTime(listJDate.SelectedItem.ToString()).Year.ToString() + "'";

Hope this helps
 
Share this answer
 
v2
Comments
[no name] 18-May-13 14:33pm    
Date is already there in the database. The column name is jdate. The listbox name is listJDate. Now what do i write in the SelectedIndexChanged event?
Jameel VM 18-May-13 14:37pm    
do you want to convert listbox item to Datetime?
Jameel VM 18-May-13 14:46pm    
from where you try to populate dates in to listbox?
[no name] 18-May-13 14:46pm    
No, Listbox items already are date values. I want to convert them to string, so my select statement works and also I should be able to display only year in listbox 1.

okie, here is my form setup and purpose:

I have two listboxes, listJDate and listParty
Dates have their respective data stored in the database. When I click on date, their respective values get displayed in the second listbox.

The value of first listbox is date ex: 5/18/2013. I want only 2013 to be displayed in listJDate.

When i raise a SelectIndexChanged event for listJDate, i need to use select statement: str = "select partyname from casetab WHERE jdate='" +listJDate.SelectedItem.ToString() + "'";

The above select statement doesnt execute because date has to be converted to string in the select statement too.

Please help!
Jameel VM 18-May-13 14:56pm    
I have updated my answer..Please try this..When you try to post a question please try to eloborate..
Regarding item 2.
You can use the Parse functionality of the DateTime class:
C#
string datetime_as_string = "<Your date time string here>";
DateTime datetime;
try {
    datetime = DateTime.Parse(datetime_as_string);
}
catch(FormatException fEx) {
    // handle the failure here, the string was not in a valid format
}
 
Share this answer
 
Comments
[no name] 18-May-13 14:42pm    
M getting this error:The string was not recognized as a valid DateTime. There is an unkown word starting at index 0
[no name] 18-May-13 14:42pm    
I used this in the form load event:

string showYear = "yyyy";
DateTime dt;
try
{
dt = DateTime.Parse(showYear);
LoadDate(showYear);
}

catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
[no name] 18-May-13 14:55pm    
Mostly because "yyyy" is not a valid DateTime. A valid DateTime is something like "5/18/2013".
Jameel VM 18-May-13 14:48pm    
the error is because you are trying to convert invalid date format to datetime
[no name] 19-May-13 8:21am    
Thank you that string statement works as of now. but when i load the listview, i get the complete date. i tried passing datetime in the void fucntion. but still in vain.

here is my code:


try
{
OleDbConnection cs = new OleDbConnection();
cs.ConnectionString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\lms\ssc.accdb;");

cs.Open();
DataTable dt = new DataTable();
DataSet ds = new DataSet();

ds.Tables.Add(dt);

OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM casetab", cs);
da.Fill(dt);

foreach (DataRow myRow in dt.Rows)
{
listView1.Items.Add(myRow[0].ToString());
listView1.Items[listView1.Items.Count - 1].SubItems.Add(myRow[1].ToString());

}
}

catch (Exception ex)
{
MessageBox.Show(ex.Message);

}

The first listview doesn't load at all :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900