指数超出范围。必须是非负的且小于wpf中datagrid中集合的大小 [英] index was out of range. must be nonnegative and less than the size of the collection in datagrid in wpf

查看:112
本文介绍了指数超出范围。必须是非负的且小于wpf中datagrid中集合的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void grid_load()
{
    try
    {
        //dataGrid1.Columns.Clear();
        //dataGrid3.Visibility = Visibility.Hidden;
        addCommanNamBtn.Visibility = Visibility.Visible;
        deleteBtn.Visibility = Visibility.Hidden;
        delbtn.Visibility = Visibility.Hidden;
        comBlock.Visibility = Visibility.Hidden;
        cnamblock.Visibility = Visibility.Hidden;
        label29.Visibility = Visibility.Hidden;
        selectCnameLbl.Visibility = Visibility.Hidden;
        scientificNameLbl.Visibility = Visibility.Hidden;
        quantityLbl.Visibility = Visibility.Hidden;
        rateLbl.Visibility = Visibility.Hidden;
        othercostLbl.Visibility = Visibility.Hidden;
        sowingdateLbl.Visibility = Visibility.Hidden;
        readyDateLbl.Visibility = Visibility.Hidden;
        potsizLbl.Visibility = Visibility.Hidden;
        textBlock2.Visibility = Visibility.Hidden;
        idlbl.Visibility = Visibility.Hidden;
        editBtn.Visibility = Visibility.Hidden;
        editbtncnam.Visibility = Visibility.Hidden;
        entercnameLbl.Visibility = Visibility.Hidden;
        con.Open();
        
        SqlDataReader rd;
        SqlCommand cmod = new SqlCommand("Select catageoryId from catageory where catageoryName='" + textBlock2.Text + "'", con);
        rd = cmod.ExecuteReader();
        rd.Read();
        idlbl.Text = rd[0].ToString();
        rd.Close();
        
        SqlCommand cmd = new SqlCommand("Select commanNameId, commanName As CommanName ,date As Date from commanNametbl where catageoryId='" + idlbl.Text + "' Order By commanName ASC", con);
        cmd.ExecuteNonQuery();
        con.Close();
        SqlDataAdapter dap = new SqlDataAdapter(cmd);
        
        DataTable dt = new DataTable();
        dap.Fill(dt);
        dataGrid1.ItemsSource = dt.DefaultView;
        dataGrid1.UpdateLayout();
        dataGrid1.Columns[0].Visibility = Visibility.Hidden;
        
    }
        
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}




private void delbtn_Click(object sender, RoutedEventArgs e)
{
    try
    {
    
        string message = "Are you sure?";
        string caption = "Confirmation";
        MessageBoxButton buttons = MessageBoxButton.YesNo;
        MessageBoxImage icon = MessageBoxImage.Question;
        
        if (MessageBox.Show(message, caption, buttons, icon) == MessageBoxResult.Yes)
        {
            // MessageBox.Show("fhgfhgfghfghf");
            con.Open();
            SqlCommand ccmd = new SqlCommand("Select Count(*) from productionStock where commanNameId='" + comBlock.Text + "'", con);
            Int32 count = (Int32)ccmd.ExecuteScalar();
            con.Close();
            if (count > 0)
            {
                MessageBox.Show("This CommanName is assigned to Plants Stock");
            }
            else
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("Delete  from commanNametbl where commanNameId='" + comBlock.Text + "'", con);
               cmd.ExecuteNonQuery();
               
                con.Close();
                u1.refresh();
                grid_load();
                //dataGrid1.SelectedItems.Clear();
                comanNametextBox.Text = "";
                comNametextBlock.Text = "";
                MessageBox.Show("Record Deleted Sucessfully!");
                
            }
            
        }
        else
        {
    
        }
        
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

推荐答案

可能在这里你收到错误:

Probably, you are getting the error here:
idlbl.Text = rd[0].ToString();

最可能的原因是没有符合您条件的记录:

And the most likely reason is that there are no records which match your condition:

SqlCommand cmod = new SqlCommand("Select catageoryId from catageory where catageoryName='" + textBlock2.Text + "'", con);

因此,请检查您的文本框是否包含您认为它的作用,并确保数据库确实具有匹配的行。事实上,最好在运行时检查这个:

So check that your textbox contains what you think it does, and that the DB does have at leats one row which matches. In fact, it's best to check this at run time as well:

SqlCommand cmod = new SqlCommand("Select catageoryId from catageory where catageoryName='" + textBlock2.Text + "'", con);
rd = cmod.ExecuteReader();
if (rd.Read())
    {
    idlbl.Text = rd[0].ToString();
    ...

但请不要这样做!不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询:

But please, don't do it like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead:

SqlCommand cmod = new SqlCommand("Select catageoryId from catageory where catageoryName=@CN", con);
cmod.Parameters.AddWithValue("@CN", textBlock2.Text);
rd = cmod.ExecuteReader();
if (rd.Read())
    {
    idlbl.Text = rd[0].ToString();


这篇关于指数超出范围。必须是非负的且小于wpf中datagrid中集合的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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