这是我的前台代码:
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" PageSize="5" Width="469px"  BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal">
                <Columns>
                    <asp:TemplateField HeaderText="学生信息">
                        <ItemTemplate>
                            姓名:<asp:Label ID="Label4" runat="server"  Text='<%# Eval("sd_name") %>'></asp:Label><br />
                            学号:<asp:Label ID="Label5" runat="server"  Text='<%# Eval("sd_no") %>'></asp:Label><br />
                            课程号:<asp:Label ID="Label2" runat="server"  Text='<%# Eval("cr_no") %>'></asp:Label>
                        </ItemTemplate>
                        <ItemStyle Width="150px" />
                   
                    
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            成绩
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                            <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="TextBox2"
                                ErrorMessage="超出范围" MaximumValue="100" MinimumValue="0"  Type="Double"></asp:RangeValidator>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
                <HeaderStyle BackColor="#9EAADA" Font-Bold="True" ForeColor="#F7F7F7" />
                <AlternatingRowStyle BackColor="#F7F7F7" />
            </asp:GridView>
后代码是这样:                   
                foreach (GridViewRow row in GridView2.Rows)
                    {
                        Label lbname = (Label)row.Cells[0].FindControl("Label4");//学生姓名
                        string name = Convert.ToString(lbname.Text);
                       Label cno = (Label)row.Cells[0].FindControl("Label2");//课程叼
                       string c_no = Convert.ToString(cno.Text); 
                        Label lb = (Label)row.Cells[0].FindControl("Label5");//学号
                        TextBox fs = (TextBox)row.Cells[1].FindControl("TextBox2");//分数
                        string stuno = Convert.ToString(lb.Text);
                        Single fenshu;
                        if (fs.Text != "")
                        {                            fenshu = Convert.ToSingle(fs.Text);                        }
                        else
                        {
                            fenshu = 0.9f;
                        }                       string insert = "update cs_cs set cs_grade=@fenshu where cr_no=@cr_no and sd_no=@stuno";
                         con.Parameters.Clear();
                         con.Parameters.Add("@fenshu", SqlDbType.Char).Value = fenshu;
                         con.Parameters.Add("@stuno", SqlDbType.Char).Value = stuno;
                         con.Parameters.Add("@cr_no", SqlDbType.Char).Value = c_no;
                         con.ExecuteNonQueryForTransaction(insert);
数据库中成绩那个字段类型是:float
请问题为何那个成绩字段的值不能成功添加到数据库中?

解决方案 »

  1.   

    数据库的float对应的是double类型的吧,存入好象要强制转换一下!
      

  2.   

    如果用convert.todouble
    系统提示:
     CS0266: 无法将类型“double”隐式转换为“float”。存在一个显式转换(是否缺少强制转换?)
      

  3.   

                            float fenshu; 
                            if (fs.Text != "") 
                            {                             fenshu = float.parse(fs.Text);                         } 
                            else 
                            { 
                                fenshu="";                        } 
      

  4.   

    还是不行,就是知道是数据类型引起的,不知道问题出在何处,
    还是不执行if (fs.Text != "") 
                            {                             fenshu = float.parse(fs.Text);                         } 
    这一段代码
      

  5.   

    Debug 一下看看你的值得到没 
      

  6.   

    没有,不行的,
    还不执行这些,
    if (fs.Text != "") 
                            {                             fenshu = float.parse(fs.Text);                         } 我初步怀疑是不是这个textbox没有起到作用啊
      

  7.   

    fs.Text  这里面的值都是空的当然 执行下面的了,
      

  8.   

    如果是这个话:
    float fenshu; 
                            if (fs.Text != "") 
                            {                             fenshu = float.parse(fs.Text);                         } 
                            else 
                            { 
                                fenshu=float.parse(fs.Text);                        } 
    系统会提示错误:输入字符串的格式不正确。
      

  9.   

    else里执行了,也就是float.parse("")了 当然报错了.....
      

  10.   

    if还是没执行 ,TextBox fs = (TextBox)row.Cells[1].FindControl("TextBox2");//分数 
    fs没有得到你前台给的值 得到的是空
      

  11.   

    (TextBox)row.Cells[1].FindControl("TextBox2");问题就在这句上