列表框空未显示数据 [英] List box empty not shown data

查看:103
本文介绍了列表框空未显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



i我正在使用vs2008和sql数据库。

i有一个组合框,其中有有两种选择:黄金和白银

当我选择白银价值然后它显示数据库中的白银价值,如果我选择黄金则其显示数据库中的黄金价值。 />
但它无效。



DAtabase:RPSJDB

表名:ItemTable

列名:txtItemId(pk,int,NotNull)

cmbItemType(nvarcha(50),不是Null)

txtItemCode(nvarcha(50),而不是Null)

txtItemDescription(nvarcha(50),而不是Null)

txtLaborCharge(float,Null)

txtItemPercent(float,Null)


当itemType = Gold时,
然后所有Itemdescription =在列表框中显示





所有编码都是工作正常,没有任何错误。请告诉我我的代码中有什么问题。



 private void cmboItemType()// for combobox 
{
string connstr = @Server = .\SQLEXPRESS; Initial Catalog = RPSJDB; Integrated Security = True;最大池大小= 100;
SqlConnection conn = new SqlConnection(connstr);
SqlCommand cmd = new SqlCommand(SELECT Distinct * FROM ItemTable,conn);
// SqlCommand cmd = new SqlCommand(SELECT DISTINCT cmbItemType FROM ItemTable,conn);
conn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
ArrayList ItemStore = new ArrayList();

while(sdr.Read())
{
ItemStore.Add(new AddValue(sdr.GetString(1),sdr.GetInt32(0)));
}
sdr.Close();
conn.Close();
cmbItemType.DataSource = ItemStore;
cmbItemType.DisplayMember =Display;
this。 cmbItemType.ValueMember =Value;
ItemHaveBeenAdded = true;

}
public class AddValue
{
private string Displa YM;
private long Valm;
public AddValue(string Display,long Value)
{
Displaym = Display;
Valm =价值;
}
公共字符串显示
{
get {return Displaym; }
}
public long Value
{
get {return Valm; }
}

}
private void cmbItemType_SelectedIndexChanged(object sender,EventArgs e)
{
if(this.ItemHaveBeenAdded)
lstboxItem( );
}
private void lstboxItem()
{
string connstr = @Server = .\SQLEXPRESS; Initial Catalog = RPSJDB; Integrated Security = True; Max Pool Size = 100 ;
SqlConnection conn = new SqlConnection(connstr);
this.lstbItemDescription.Items.Clear();
SqlCommand cmd = new SqlCommand
(SELECT cmbItemType FROM ItemTable where cmbItemType =+ cmbItemType.SelectedValue,conn);
conn.Open();
SqlDataReader sdrItem = cmd.ExecuteReader();
while(sdrItem.Read())
{
this.lstbItemDescription.Items.Add(sdrItem.GetString(1));
}
sdrItem.Close();
conn.Close();
}
private void button1_Click(object sender,EventArgs e)
{
this.Close();
}

解决方案

您是否使用Visual Studio Interactive Debugger逐步执行代码?



我认为问题是由访问 sdrItem.GetString(1)而不是 sdrItem.GetString(0)引起的 in lstboxItem()





< pre lang =cs> while (sdrItem.Read())
{
this .lstbItemDescription.Items.Add(sdrItem.GetString( 0 ));
}





我更愿意访问

 sdrItem [  cmbItemType]。ToString 



为了便于阅读和更容易调试。



此外,Select语句应该在 cmbItemType.SelectedValue 之前和之后有撇号值因为它是数据库中的NVarChar数据类型。



 SqlCommand cmd = new SqlCommand 
SELECT cmbItemType FROM ItemTable其中cmbItemType =' + cmbItemType.SelectedValue + ',conn);











如果仍未将结果输入ListBox,请在时设置断点(sdrItem.Read()) 然后使用 F11 键逐步执行代码以查看 Items.A dd 语句被执行。如果没有执行,则由于选择语句而不返回任何行。使用交互式窗口查看 cmbItemType.SelectedValue 的值,然后确保数据库包含与该值匹配的条目。


在编译/ Dubugging时检查cmbItemType.SelectedValue的值,

然后检查this.lstbItemDescription.Items.Add(sdrItem.GetString(1));它在while循环中添加项目....


AutopostBack = True;



它给出了错误

错误当前上下文中不存在AutopostBack名称

如何设置它。


Dear All,

i am using vs2008 and sql database.
i have a combobox in which there are two option : gold and silver
when i am select the silver value then its show the silver value from the database and if i am select the gold then its show the gold value from the database.
but its not working.

DAtabase :RPSJDB
Table Name: ItemTable
Column Name: txtItemId(pk,int,NotNull)
cmbItemType(nvarcha(50),not Null)
txtItemCode(nvarcha(50),not Null)
txtItemDescription(nvarcha(50),not Null)
txtLaborCharge(float,Null)
txtItemPercent(float,Null)

when itemType=Gold then all the Itemdescription = show in list box


all the coding is working fine without any error.so pls tell me what is the problem in my code.

private void cmboItemType()    //for combobox 
        {
            string connstr = @"Server=.\SQLEXPRESS ;Initial Catalog=RPSJDB;Integrated Security=True; Max Pool Size=100";
            SqlConnection conn = new SqlConnection(connstr);
            SqlCommand cmd = new SqlCommand("SELECT Distinct * FROM ItemTable", conn);
            //SqlCommand cmd = new SqlCommand("SELECT DISTINCT cmbItemType FROM ItemTable", conn);
            conn.Open();
            SqlDataReader sdr = cmd.ExecuteReader();
            ArrayList ItemStore = new ArrayList();
            
            while (sdr.Read())
            {
                ItemStore.Add(new AddValue(sdr.GetString(1), sdr.GetInt32(0)));
            }
            sdr.Close();
            conn.Close();
            cmbItemType.DataSource = ItemStore;
            cmbItemType.DisplayMember = "Display";
            this.cmbItemType.ValueMember = "Value";
            ItemHaveBeenAdded = true;						
            
        }
        public class AddValue
        {
            private string Displaym;
            private long Valm;
            public AddValue(string Display,long Value)
            {
                Displaym = Display;
                Valm = Value;
            }
            public string Display
            {
                get { return Displaym; }
            }
            public long Value
            {
                get { return Valm; }
            }
         
        }
        private void cmbItemType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ItemHaveBeenAdded)
            lstboxItem();
        }
        private void lstboxItem()
        {
            string connstr = @"Server=.\SQLEXPRESS ;Initial Catalog=RPSJDB;Integrated Security=True; Max Pool Size=100";
            SqlConnection conn = new SqlConnection(connstr);
            this.lstbItemDescription.Items.Clear();
            SqlCommand cmd = new SqlCommand
                ("SELECT cmbItemType FROM  ItemTable Where cmbItemType =" + cmbItemType.SelectedValue, conn);
            conn.Open();
            SqlDataReader sdrItem = cmd.ExecuteReader();
            while (sdrItem.Read())
            {
                this.lstbItemDescription.Items.Add(sdrItem.GetString(1));
            }
            sdrItem.Close();
            conn.Close();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

解决方案

Have you used the Visual Studio Interactive Debugger to step through your code?

I think the problem is caused by accessing sdrItem.GetString(1) instead of sdrItem.GetString(0) in lstboxItem().


while (sdrItem.Read())
{
    this.lstbItemDescription.Items.Add(sdrItem.GetString(0));
}



I prefer to access as

sdrItem["cmbItemType"].ToString


for readability and easier debugging.

Also, the Select statement should have apostrophes before and after the cmbItemType.SelectedValue value since it is a NVarChar data type in the database.

SqlCommand cmd = new SqlCommand 
   ("SELECT cmbItemType FROM  ItemTable Where cmbItemType ='" + cmbItemType.SelectedValue + "'", conn);






If you still do not get results into the ListBox, set a breakpoint at while (sdrItem.Read()) and then use the F11 key to step through your code to see if the Items.Add statement gets executed. If it does not get executed, then no rows are returned as a result of your Select statement. Use the Interactive Window to look at the value of cmbItemType.SelectedValue and then ensure that the database contains entries that match that value.


check the value of cmbItemType.SelectedValue when compiling/Dubugging,
and then check this.lstbItemDescription.Items.Add(sdrItem.GetString(1)); it adding the item in while loop ....


AutopostBack=True;

it gives a error
Error The name 'AutopostBack' does not exist in the current context
how to set it.


这篇关于列表框空未显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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