无法将类型'bool'隐式转换为'string' [英] Cannot implicitly convert type 'bool' to 'string'

查看:750
本文介绍了无法将类型'bool'隐式转换为'string'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我想要特定日期数据我写一些代码,而且我也使用sql server 2008

列类型rdate = datetime一些我在一个文本框中给出日期文本框

日期数据可以显示gridview但是这里有一些错误所以请帮助我

i我给了源代码



  string  dateString = TextBox6.Text;  //  <  - 有效 
string format = ddd dd MMM h:mm tt yyyy;
DateTime dateTime;
TextBox6.Text = DateTime.TryParseExact(dateString,format,CultureInfo.InvariantCulture,DateTimeStyles.None, out dateTime);
SqlConnection con = new SqlConnection( 数据Source = NSYS1 \\SQLEXPRESS; Initial Catalog = agfl; connect timeout = 30; Integrated Security = True);
con.Open();
string strQuery = select * from stf其中rdate = @ some;
SqlCommand cmd = new SqlCommand(strQuery,con);
cmd.CommandType = CommandType.Text;
TextBox6.Text = TextBox6.Text.Trim();
cmd.Parameters.AddWithValue( @ some,TextBox6.Text);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
gvuser.DataSource = dt;
gvuser.DataBind();
con.Close();

解决方案

看看那一行:

 TextBox6.Text = DateTime.TryParseExact(dateString,format,CultureInfo.InvariantCulture,DateTimeStyles.None, out  dateTime); 



TryParse / TryParseExact返回一个布尔值:布尔返回值表示解析是成功还是失败。

您应该像以下一样使用它:

  if (!DateTime.TryParseExact(TextBox6.Text,format,CultureInfo.InvariantCulture,DateTimeStyles.None, out  dateTime))
{
// 显示一条消息给用户OR
// 使用默认值或
// 做你认为有用的事情l当TextBox6中的数据不是日期
};



此外,在代码中使用更远的dateTime变量而不是TextBox6。文本!

Hello ,

I want to particular date data i write some code and also iam using sql server 2008
the column type rdate=datetime some i am given the date in one textbox the textbox
date data can be display the gridview but here is the some error so please help me
i am given the source code

string dateString = TextBox6.Text; // <-- Valid
            string format = "ddd dd MMM h:mm tt yyyy";
            DateTime dateTime;
            TextBox6.Text = DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime);
            SqlConnection con = new SqlConnection("Data Source=NSYS1\\SQLEXPRESS;Initial Catalog=agfl;connect timeout=30;Integrated Security=True");
            con.Open();
            string strQuery = "select * from stf where  rdate =@some";
            SqlCommand cmd = new SqlCommand(strQuery, con);
            cmd.CommandType = CommandType.Text;
            TextBox6.Text = TextBox6.Text.Trim();
            cmd.Parameters.AddWithValue("@some", TextBox6.Text);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            gvuser.DataSource = dt;
            gvuser.DataBind();
            con.Close();

解决方案

Look at that line:

TextBox6.Text = DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime);


TryParse / TryParseExact return a Boolean: the boolean return value indicates whether the parsing succeeded or failed.
You ought to use it like:

if (!DateTime.TryParseExact(TextBox6.Text, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
{
    //show a message to the user OR 
    //use a default value OR 
    //do what ever you think is useful when the data in TextBox6 are not a date
};


Also, use the dateTime variable farther down in your code instead of TextBox6.Text!


这篇关于无法将类型'bool'隐式转换为'string'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆