Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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);
    }
}

C#
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());
    }
}
Posted
Updated 28-Jun-14 21:08pm
v2
Comments
DamithSL 29-Jun-14 3:07am    
in which line you get this exception?
Member 10912163 29-Jun-14 3:21am    
when i call grid_load();
in delbtn
DamithSL 29-Jun-14 4:15am    
learn how to debug and find which line line exception throwing and which objects involved. then you can find solution.
Kornfeld Eliyahu Peter 29-Jun-14 3:13am    
It seems that your SQL query doe snot return any data. You have to debug it to see exactly why...
Member 10912163 29-Jun-14 3:23am    
Sql Query Executed correctly but problem is when i call grid_load();
to refresh the datagrid.then exception occurs.

1 solution

Probably, you are getting the error here:
C#
idlbl.Text = rd[0].ToString();
And the most likely reason is that there are no records which match your condition:
C#
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:
C#
SqlCommand cmod = new SqlCommand("Select catageoryId from catageory where catageoryName='" + textBlock2.Text + "'", con);
rd = cmod.ExecuteReader();
if (rd.Read())
    {
    idlbl.Text = rd[0].ToString();
    ...
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:
C#
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();
 
Share this answer
 
Comments
Member 10912163 29-Jun-14 3:36am    
I check the reuslt of that query it is working correctly.By checking the value of idlbl.text in MessageBox..
Remember.First time datgrid shows data corectly but when i select row from datagrid and delete it through delbtn .it deletes the coresponding row but when i call grid_load(); function it gives Exception...
Kornfeld Eliyahu Peter 29-Jun-14 6:03am    
How do you refresh your page? Maybe the second time idlbl.Text is empty? You HAVE to debug it!!!
Member 10912163 29-Jun-14 6:09am    
I am calling this funtion grid_load() in page_load()..But how i can update datagrid after deletion instead of that..Or you can help me reload datagrid without page refresh();??
Kornfeld Eliyahu Peter 29-Jun-14 6:12am    
It's not clear what are you doing, but it obvious that when the second time you call grid_load, something is different - check what!
Member 10912163 29-Jun-14 9:05am    
i have problem with this dataGrid1.ItemsSource = dt.DefaultView ;
plz tell me how to solve it.When i call grid_load(); it gives exception here whole query is this.
SqlCommand cmd = new SqlCommand("Select commanNameId AS ID, commanName As CommanName ,date As Date from commanNametbl where catageoryId='" + idlbl.Text + "' Order By commanName ASC", con);
cmd.ExecuteNonQuery();

SqlDataAdapter dap = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
dap.Fill(dt);
ItemsSource = dt.DefaultView ;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900