private void ShowSchoolView1(string areaid)
    {        this.GridMessage.Visible = false;
        SqlConnection conn = new SqlConnection("server=172.16.1.248;uid=sa;password='$eszaq!23w';database=JxtFee");
        conn.Open();
        SqlCommand command = new SqlCommand("Ptj_FeeMobPermon_sel", conn);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@seldate", SqlDbType.VarChar, 20);
        command.Parameters.Add("@areaid", SqlDbType.Int);
        command.Parameters.Add("@schid", SqlDbType.Int);
        command.Parameters["@seldate"].Value = Convert.ToDateTime(this.DropYear.SelectedValue + "-" + this.DropMonth.SelectedValue + "-01");
        command.Parameters["@areaid"].Value = areaid;
        command.Parameters["@schid"].Value = DBNull.Value;
        SqlDataAdapter sda = new SqlDataAdapter(command);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        this.ShowSchoolView.DataSource = ds;
        this.ShowSchoolView.DataBind();    }
在执行的时候会出现从字符串向 datetime 转换时失败的问题
 sda.Fill(ds);
这行出错,这是怎么回事该怎么解决

解决方案 »

  1.   

    command.Parameters.Add("@seldate", SqlDbType.DateTime, 8); 
      

  2.   

     command.Parameters.Add("@seldate", SqlDbType.VarChar, 20); 
    这里错了 把类型改成datetime
      

  3.   

    command.Parameters.Add("@seldate", SqlDbType.VarChar, 20); 
    command.Parameters["@seldate"].Value = Convert.ToDateTime(this.DropYear.SelectedValue + "-" + this.DropMonth.SelectedValue + "-01"); 设置类型是varchar,给的值却是datetime
    改为
    command.Parameters.Add("@seldate", SqlDbType.VarChar, 20); 
    command.Parameters["@seldate"].Value = this.DropYear.SelectedValue + "-" + this.DropMonth.SelectedValue + "-01"; 

    command.Parameters.Add("@seldate", SqlDbType.DateTime, 20); 
    command.Parameters["@seldate"].Value = Convert.ToDateTime(this.DropYear.SelectedValue + "-" + this.DropMonth.SelectedValue + "-01"); 
      

  4.   

    command.Parameters.Add("@seldate", SqlDbType.DateTime, 8);
    command.Parameters["@seldate"].Value = Convert.ToDateTime(this.DropYear.SelectedValue + "-" + this.DropMonth.SelectedValue + "-01").ToShortDateString();
      

  5.   

    command.Parameters.Add("@seldate", SqlDbType.VarChar, 20); 
    改为command.Parameters.Add("@seldate", SqlDbType.DateTime,8); 
            command.Parameters["@seldate"].Value = Convert.ToDateTime(this.DropYear.SelectedValue + "-" + this.DropMonth.SelectedValue + "-01"); 
    没必要强制转换  
    command.Parameters["@seldate"].Value = this.DropYear.SelectedValue + "-" + this.DropMonth.SelectedValue + "-01"; 
      

  6.   

    最关键的就是Convert.ToDateTime(this.DropYear.SelectedValue + "-" + this.DropMonth.SelectedValue + "-01"); 出错是吧,
    请问这个的值this.DropYear.SelectedValue + "-" + this.DropMonth.SelectedValue + "-01" 是多少???请检查是否有空格之类的,务必使其格式满足 "yyyy-MM-dd" 还有你上面的明明定义的是Varchar类型的数据,下面却要转DATATIME,,~~~~!
      

  7.   

    存储过程是这样的
    ALTER PROCEDURE [dbo].[Ptj_FeeMobPermon_Sel]
    @seldate  varchar(20),
    @areaid int,
    @schid    int=null
    AS
    BEGIN
    if @areaid is not null and @schid is null
    begin
     --指定地区
    select a.schid,b.schname,
               case when a.mobtype=1 then '移动' when a.mobtype=2 then '联通' when a.mobtype=3 then '小灵通' end as mobtype,
    sum(a.mobnum) as mobnum,sum(a.sucnum) as sucnum,sum(a.failnum) as failnum 
    from dbo.Tj_FeeMobPermon a,dbo.Fee_School b where a.schid=b.schid
    and datediff(mm,a.countmonth,@seldate)=0 and left(a.areaid,4)=@areaid
    group by a.schid,b.schname,a.mobtype 
    end
    else if @schid is not null
    begin
    --指定学校
            select a.classid,b.classname,
            case when a.mobtype=1 then '移动' when a.mobtype=2 then '联通' when a.mobtype=3 then '小灵通' end as mobtype,
    sum(a.mobnum) as mobnum,sum(a.sucnum) as sucnum,sum(a.failnum) as failnum 
    from dbo.Tj_FeeMobPermon a,dbo.Fee_class b
    where datediff(mm,a.countmonth,@seldate)=0 and a.schid=@schid and a.classid=b.classid
    group by a.classid,b.classname,a.mobtype
    end
    END
      

  8.   


    存储过程里是 @seldate  varchar(20), 
      

  9.   

    存储过程里面是@seldate  varchar(20)啊你要让里外统一就对了...
    外面
    command.Parameters.Add("@seldate", SqlDbType.***
    command.Parameters["@seldate"].Value = ***
    里面
    @seldate  ***
    ***要统一!~
      

  10.   

    日期类型的还是用DATETIME类型会好,以防数据不统一,方便以后维护