如何在asp.net c#sqlserver 2005中读取数据库值。 [英] How to read Database values in asp.net c# sqlserver 2005.

查看:68
本文介绍了如何在asp.net c#sqlserver 2005中读取数据库值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在asp.net c#sqlserver 2005中读取数据库值。

-------------------------- ---------------------------



嗨frnds,
我的网页上有


我有一个下拉列表来读取数据库中的值并显示在标签中。

在这个下拉列表中它有部门代码。



这是我的代码。



How to read Database values in asp.net c# sqlserver 2005.
-----------------------------------------------------

Hi frnds,

on my webpage i have a dropdownlist to read the values from database and display in labels.
In this dropdownlist it has department codes.

This is my code.

protected void DDLDept_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection conDisp = new SqlConnection(_connString);
        SqlCommand cmdDisp = new SqlCommand();
        cmdDisp.Connection = conDisp;

        cmdDisp.CommandText = "Select * from Departments where Dept_code='" + DDLDept.SelectedValue + "'";

        using(conDisp)
        {
            conDisp.Open();
            SqlDataReader readerDisp = cmdDisp.ExecuteReader();
            if (readerDisp.Read())
            {

               

                lblDepartmentDesc.Text = readerDisp["Dept_desc"].ToString();
                lblTelNo.Text = readerDisp["TelephoneNo"].ToString();
                lblDepartmentManager.Text = readerDisp["Dir_Mgr"].ToString();

            }

            else
            {

                lblDepartmentDesc.Text = "Not available";
                lblTelNo.Text = "Not available";
                lblDepartmentManager.Text = "Not available";
            }

            

        }
       
    }



这是完美的工作。但问题是,如果从下拉列表中选择的ddept和所选部门的详细信息不可用。



在标签中它必须显示不可用



请帮帮我..谢谢


This is working perfect. But the problem is, if the ddept selected from dropdown and selected department details are not available.

In labels it must display "Not Available"

Please help me..Thanks

推荐答案

如果忽略了else部分,当你不认为某个值时如果选择了,那么最明显的答案是你的数据库包含一个空值并且正在被选中。

所以......在开始访问数据库之前尝试添加一个检查 - 检查DDL并制作确定它有一个选定的值!

If it is ignoring the "else" part when you don't think a value is selected, then the most obvious answer is that your DB contains a blank value and that is being selected.
So... try adding a check before you start accessing the DB - check teh DDL and make sure it has a selected value!
if (DDLDept.SelectedValue != null)
   {
   ...
   if (readerDisp.Read())
      {
      ...
      return;
      }
   }
lblDepartmentDesc.Text = "Not available";
lblTelNo.Text = "Not available";
lblDepartmentManager.Text = "Not available";


尝试如下...

Try like below...
if (readerDisp == null || !readerDisp.HasRows) {
    // Do something
    lblDepartmentDesc.Text = "Not available";
    lblTelNo.Text = "Not available";
    lblDepartmentManager.Text = "Not available";
}
else
{
    if (readerDisp.Read())
    {
        lblDepartmentDesc.Text = readerDisp["Dept_desc"].ToString();
        lblTelNo.Text = readerDisp["TelephoneNo"].ToString();
        lblDepartmentManager.Text = readerDisp["Dir_Mgr"].ToString(); 
    }
}


这篇关于如何在asp.net c#sqlserver 2005中读取数据库值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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