简体   繁体   English

无法从C#中的列表中检索数据

[英]Unable to retrieve data from list in C#

I'm designing a WinForms desktop application using C#.Net and MSSQL server. 我正在使用C#.Net和MSSQL服务器设计WinForms桌面应用程序。 In the following class I read read the data from a stored procedure using sqlDataReader and I insert it in a List<> . 在下面的类中,我读取了使用sqlDataReader从存储过程中读取数据,并将其插入List<>

Later I want to show the list values in textboxes . 后来我想在textboxes显示列表值。 My problem is that nothing is shown in the texboxes when I retrieve data from the list. 我的问题是,当我从列表中检索数据时,texbox中没有显示任何内容。

My stored procedure: 我的存储过程:

create proc spDispMaterialPrice
@materialName nvarchar(100)
as
begin 
select unitPrice,carbohydrate,protein,fat,humidity,minerals from materialPrice where 
materialName = @materialName
end 

Class: 类:

    //display data retrived according to material combobox to user when form loads
    public List<string> displayMaterial()
    {
        List<string> materialList = new List<string>();
        string conString = ConfigurationManager.ConnectionStrings["secaloFormulaCS"].ToString();
        using (SqlConnection sqlCon = new SqlConnection(conString))
        using (SqlCommand sqlCmd = new SqlCommand("spDispMaterialPrice", sqlCon))
        {
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCon.Open();
            sqlCmd.Parameters.AddWithValue("materialName", MaterialName);                
            SqlDataReader sqlDr = sqlCmd.ExecuteReader();
            while (sqlDr.Read())
            {
                materialList[0] = sqlDr["unitPrice"].ToString();
                materialList[1] = sqlDr["carbohydrate"].ToString();
                materialList[2] = sqlDr["protein"].ToString();
                materialList[3] = sqlDr["fat"].ToString();
                materialList[4] = sqlDr["humidity"].ToString();
                materialList[5] = sqlDr["minerals"].ToString();
            }
            sqlCon.Close();
            sqlCon.Dispose();
        }
       return materialList;
    }
}

} }

winform : winform:

        private void AppForm_Load(object sender, EventArgs e)
        {
        this.materialPriceTableAdapter.Fill(this.secaloFormulaDataSet.materialPrice );
        //panel1.Hide();

        Classes.Prices prices = new Classes.Prices(comboBox1.SelectedItem.ToString());
        List<string> items = prices.displayMaterial();
        textBox3.Text = items[0];

You are using a List as if it is an array. 您正在使用List,就好像它是一个数组。 Without changing to much of your code with the following change your code might work if there is one record selected from your table, otherwise it shows the last record that matches the materialname. 如果从表中选择了一条记录,则无需通过以下更改更改大部分代码,否则它将显示与材料名称匹配的最后一条记录。

public string[] displayMaterial()
{
    var materialList = new string[6];

and in your AppForm_Load 在你的AppForm_Load中

string[] items = prices.displayMaterial();

Read the Commonly Used Collection Types from MSDN Development Fundamentals 阅读MSDN Development Fundamentals中 常用的集合类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM