变量和方法
我试图更好地理解如何声明变量(字符串)以及方法如何工作。我正在尝试将日期(从日历扩展程序)重新格式化为字符串,并将其作为参数传递到填充网格视图的查询中。 (这与我的上一个问题有关。) 转换语句如下所示:
string s_apptdate = apptDate_CalendarExtender.SelectedDate.ToString("yyyyMMdd");
应该放在下面的方法中吗?或者以它自己的方式?当我将其放入下面的方法中时,出现错误“方法‘ToString’没有重载需要 1 个参数” 我的方法看起来像这样
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();
}
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 = 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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 SelectedDate 属性可能是
DateTime?
(或Nullable
),在这种情况下,您必须在检查 SelectedDate 是否有值后执行此操作
Your SelectedDate property is probably a
DateTime?
(orNullable<DateTime>
) in wich case you have to doafter checking if SelectedDate has a value