开发者

Variables and methods

I'm trying to understand better how to declare a variable (string) and how methods work. I'm trying to reformat a date (from a calendarextender) into a string and pass it as a parameter into a query that populates a gridview. (This is related to my previous question.) The converting statement looks like this:

string s_apptdate开发者_JAVA百科 = apptDate_CalendarExtender.SelectedDate.ToString("yyyyMMdd");

Should it go in the method below? Or in a method all it's own? When I put it in the method below, I get an error "No overload for method 'ToString' takes 1 arguments" My method looks like this

private void query1() 
{
    string s_apptdate = "07/15/2011";
    SqlConnection conn = new SqlConnection("Data Source=*****;Initial Catalog=*****;Persist Security Info=True;User ID=sa;Password=*****");
    string command = "SELECT column1, column2 FROM table where appt_date = '" + s_apptdate + "'";
    SqlDataAdapter comm = new SqlDataAdapter(command, conn);
    DataSet ds = new DataSet();
    comm.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();
}


Your SelectedDate property is probably a DateTime? (or Nullable<DateTime>) in wich case you have to do

apptDate_CalendarExtender.SelectedDate.Value.ToString("yyyyMMdd");

after checking if SelectedDate has a value

string s_apptdate;
if (apptDate_CalendarExtender.SelectedDate.HasValue)
  s_apptdate = apptDate_CalendarExtender.SelectedDate.Value.ToString("yyyyMMdd");
else
  s_apptdate = string.Empty;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜