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 MaruthiPosted Jul 2, 2014, 2:00 PM
Ramesh MaruthiPosted Jul 2, 2014, 1:38 PM
if my thinking is correct you are getting error at execute scalar right ?
junaid khurshidPosted Jul 2, 2014, 11:24 PM
Ramesh MaruthiPosted Jul 2, 2014, 3:29 PM
ccmd.Parameters.Add(new SlParameter("@retval",sqlDbType.Int){Direction = ParameterDirection.ReturnValue});
int k = (int) cmd.Parameters["@retval"].Value;
Ramesh MaruthiPosted Jul 2, 2014, 3:25 PM
junaid khurshidPosted Jul 2, 2014, 3:15 PM
junaid khurshidPosted Jul 2, 2014, 2:51 PM
Ramesh MaruthiPosted Jul 2, 2014, 2:43 PM
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 khurshidPosted Jul 2, 2014, 2:33 PM
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 MaruthiPosted Jul 2, 2014, 2:27 PM
Int32 count = (Int32)ccmd.ExecuteScalar();
and please let me know does productionStock has a trigger ?
junaid khurshidPosted Jul 2, 2014, 2:23 PM
Ramesh MaruthiPosted Jul 2, 2014, 2:18 PM
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 khurshidPosted Jul 2, 2014, 2:08 PM
red text
dataGrid1.ItemsSource = ds.Tables[0];
I think on ds .error is cannot implicitly convert.....
Ramesh MaruthiPosted Jul 2, 2014, 1:54 PM
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 khurshidPosted Jul 2, 2014, 1:50 PM
when data grid is reload
junaid khurshidPosted Jul 2, 2014, 1:42 PM