I got exception on deletion of data in datagrid .
My xaml code is this for data grid:
My grid load function is this:
private void grid_load()
{
try
{
con.Open();
string CmdString = string.Empty;
SqlDataReader rd;
SqlCommand cmod = new SqlCommand("Select catageoryId from catageory where catageoryName='" + textBlock2.Text + "'", con);
rd = cmod.ExecuteReader();
if (rd.Read())
{
idlbl.Text = rd[0].ToString();
rd.Close();
CmdString = "Select commanNameId, commanName ,date from commanNametbl ";
SqlCommand cmd = new SqlCommand(CmdString, con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable("Tables");
sda.Fill(dt);
dataGrid1.ItemsSource = dt.DefaultView;
con.Close();
//MessageBox.Show(ex.ToString());
}
else
{
rd.Close();
}
}
{
try
{
con.Open();
string CmdString = string.Empty;
SqlDataReader rd;
SqlCommand cmod = new SqlCommand("Select catageoryId from catageory where catageoryName='" + textBlock2.Text + "'", con);
rd = cmod.ExecuteReader();
if (rd.Read())
{
idlbl.Text = rd[0].ToString();
rd.Close();
CmdString = "Select commanNameId, commanName ,date from commanNametbl ";
SqlCommand cmd = new SqlCommand(CmdString, con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable("Tables");
sda.Fill(dt);
dataGrid1.ItemsSource = dt.DefaultView;
con.Close();
//MessageBox.Show(ex.ToString());
}
else
{
rd.Close();
}
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
MessageBox.Show(ex.Message);
}
}
My delete button code is this:
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)
{
if (dataGrid1.SelectedValue != null)
{
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();
comanNametextBox.Text = "";
comNametextBlock.Text = "";
MessageBox.Show("Record Deleted Sucessfully!");
grid_load();
}
}
else
{
MessageBox.Show("Select values from Table");
}
}
else
{
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
{
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)
{
if (dataGrid1.SelectedValue != null)
{
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();
comanNametextBox.Text = "";
comNametextBlock.Text = "";
MessageBox.Show("Record Deleted Sucessfully!");
grid_load();
}
}
else
{
MessageBox.Show("Select values from Table");
}
}
else
{
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
I have got exception when i click the delete Btn ..actually when grid id reload then it gives exception

Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 2, 2014, 9:38 AM EDT
if my thinking is correct you are getting error at execute scalar right ?
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 2, 2014, 10:00 AM EDT
junaid khurshid49 reputationPosted Jul 2, 2014, 9:42 AM EDT
junaid khurshid49 reputationPosted Jul 2, 2014, 9:50 AM EDT
when data grid is reload
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 2, 2014, 9:54 AM EDT
CmdString = "Select commanNameId, commanName ,date from commanNametbl ";
SqlCommand cmd = new SqlCommand(CmdString, con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
//DataTable dt = new DataTable("Tables");
Dataset ds = new Dataset();
sda.Fill(ds);
dataGrid1.ItemsSource = ds.Tables[0];
junaid khurshid49 reputationPosted Jul 2, 2014, 10:08 AM EDT
red text
dataGrid1.ItemsSource = ds.Tables[0];
I think on ds .error is cannot implicitly convert.....
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 2, 2014, 10:18 AM EDT
dataGrid1.ItemsSource = ds.Tables[0].AsDataView();
And please have a look at this
http://www.codeproject.com/Questions/454755/Csharp-Datagridview-update-and-delete
and in your old code please make some changes and try
Datatable dt = new Datatable();
dataGrid1.ItemsSource = dt.AsDataView();
junaid khurshid49 reputationPosted Jul 2, 2014, 10:23 AM EDT
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 2, 2014, 10:27 AM EDT
Int32 count = (Int32)ccmd.ExecuteScalar();
and please let me know does productionStock has a trigger ?
junaid khurshid49 reputationPosted Jul 2, 2014, 10:33 AM EDT
in this Int32 count = (Int32)ccmd.ExecuteScalar(); I am checking that CommanName is Assigned to a plant in productionStock or not.....
I tell you that delete query is runing correctly it deltes the record ...
but problem is there when datagrid reload or refresh.
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 2, 2014, 10:43 AM EDT
and tell me the count value here what you are getting sometimes it gives you -1, your query may work perfectly in sqlserver but not here,so please once keep a break point at count then debug and let me know the value of it
junaid khurshid49 reputationPosted Jul 2, 2014, 10:51 AM EDT
junaid khurshid49 reputationPosted Jul 2, 2014, 11:15 AM EDT
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 2, 2014, 11:25 AM EDT
Ramesh MaruthiRank #937 · 2.2K reputationPosted Jul 2, 2014, 11:29 AM EDT
ccmd.Parameters.Add(new SlParameter("@retval",sqlDbType.Int){Direction = ParameterDirection.ReturnValue});
int k = (int) cmd.Parameters["@retval"].Value;
junaid khurshid49 reputationPosted Jul 2, 2014, 7:24 PM EDT