首页 新闻 会员 周边

显示不出来价格,求大神帮助

0
悬赏园豆:20 [已解决问题] 解决于 2017-07-03 10:38

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void BTQuery_Click(object sender, EventArgs e)
    {
        string connString;
        string bookID = TBbookID.Text;

        connString = "Initial Catalog=onlineBookstore;Data Source=(local);Integrated Security=SSPI;";
       
        using (SqlConnection Con = new SqlConnection(connString))
        {
            Con.Open();
            string sql = "select bookname,sellprice from bookinfo where bookID=@bookID";
            using (SqlCommand bookinfoCommand = new SqlCommand(sql, Con))
            {
                bookinfoCommand.Parameters.Add("@bookID", SqlDbType.VarChar, 25);
                bookinfoCommand.Parameters["@bookID"].Value = bookID;
                bookinfoCommand.CommandType = CommandType.Text;
                using (SqlDataReader a = bookinfoCommand.ExecuteReader())
                {
                    try
                    {
                        while (a.Read())
                        {
                            TBbookname.Text = (string)a["bookname"];
                            TBsellprice.Text = (string)a["sellprice"];
                        }
                        Con.Close();
                    }
                    catch
                    {
                        Console.WriteLine("发生错误:");
                    }
                }
            }
        }        
    }       
}

泽峰的主页 泽峰 | 初学一级 | 园豆:48
提问于:2017-07-02 22:45
< >
分享
最佳答案
0

 TBsellprice.Text = (string)a["sellprice"];

这里打个断点debug看下这块有没有取到值,或者你数据库表里本来就是空值。

转string建议这样:

1 a["sellprice"].tostring();

延伸:

DataReader转Entity

可以使用AutoMapper工具,不要这样一个个属性赋值。

参考:http://www.cnblogs.com/kejie/p/6946228.html

收获园豆:20
~扎克伯格 | 小虾三级 |园豆:1923 | 2017-07-03 09:06

谢谢,我改成toString()结果就显示出来了

泽峰 | 园豆:48 (初学一级) | 2017-07-03 10:31
其他回答(3)
0

显示不出来调试一下不就晓得了

MrNice | 园豆:3450 (老鸟四级) | 2017-07-03 08:41
0

先查源数据有没有,是不是空的之类的,再查是不是类型转换有问题,再查显示控件是不是绑定错误之类的问题

hahanonym | 园豆:1460 (小虾三级) | 2017-07-03 09:03

数据库里有数据

支持(0) 反对(0) 泽峰 | 园豆:48 (初学一级) | 2017-07-03 10:25
0

打断点调试一下价格是不是为空,sql可以判断一下 nvl(sellprice,0) ,这样如果为空,显示为0,就不会出现显示空白的情况

无殇-x | 园豆:9 (初学一级) | 2017-07-03 10:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册