How do I access a disposed object? - c++-cli

This is where I show my form. Basically I'm editing data in a database. I get the exception at line DataGridViewRow object.
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
int selectedIndex = dataGridView1->CurrentCell->RowIndex;
if (selectedIndex >= 0)
{
MessageBox::Show("selection is" + selectedIndex);
edit->Show();
try
{
DataGridViewRow ^row = this->dataGridView1->Rows[selectedIndex];
edit->itemcodetextBox->Text = row->Cells[0]->Value->ToString();
edit->itemnametextbox->Text = row->Cells[1]->Value->ToString();
edit->stockamnttextBox->Text = row->Cells[2]->Value->ToString();
edit->pricetextBox->Text = row->Cells[3]->Value->ToString();
edit->categorytextBox->Text = row->Cells[4]->Value->ToString();
}
catch (Exception^ ex)
{
MessageBox::Show(" Error Updating Data ");
}
}

Related

How to save IP camera snapshot in Content folder using EmguCV c#

I'm using EmguCV 3.4. I need to store snapshots from IP camera periodically(5 mins). I try to stored snapshots in the Content folder (ASP.NET MVC). I got access violation exception. Help me.
My Code,
private VideoCapture _capture = null;
private Mat _frame;
public void GetSnapshot(CameraDTO cameraDTO)
{
CvInvoke.UseOpenCL = false;
try
{
_capture = new VideoCapture(cameraDTO.CameraAccessURL);
_capture.ImageGrabbed += ProcessFrame;
if (StartCapture())
{
while (_frame == null)
{
//wait untill camera ready
}
if (_frame != null && _capture != null)
{
Image<Bgr, Byte> imgeOrigenal = _frame.ToImage<Bgr, Byte>();
imgeOrigenal.Save(#ImageSavepath + #"\ImageFromCamera" + cameraDTO.camID + ".jpg");
}
}
}
catch (Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
private bool StartCapture()
{
if (_capture != null)
{
_capture.Start();
return true;
}
return false;
}
private void ProcessFrame(object sender, EventArgs e)
{
try
{
if (_capture != null && _capture.IsOpened && _capture.Ptr != IntPtr.Zero && _frame != null)
{
_frame = new Mat();
_capture.Retrieve(_frame, 0);
}
}
catch (AccessViolationException exv)
{
Console.WriteLine("ERROR: {0}", exv.Message);
return;
}
catch (Exception ex)
{
Console.WriteLine("ERROR: {0}", ex.Message);
return;
}
}
#ImageSavepath = Content folder, randomly generate the issue
The problem is that you use _frame in another thread (cross-thread manipulating). You should save the image inside FrameProcess().
private void FrameProcess(object sender, EventArgs e)
{
try
{
if (_capture != null && _capture.IsOpened && _capture.Ptr != IntPtr.Zero && _frame != null)
{
_frame = new Mat();
_capture.Retrieve(_frame, 0);
Image<Bgr, Byte> imgeOrigenal = _frame.ToImage<Bgr, Byte>();
imgeOrigenal.Save(#ImageSavepath + #"\ImageFromCamera" + cameraDTO.camID + ".jpg");
}
}
catch (AccessViolationException exv)
{
Console.WriteLine("ERROR: {0}", exv.Message);
return;
}
catch (Exception ex)
{
Console.WriteLine("ERROR: {0}", ex.Message);
return;
}
}

How to search the record in the sql adapter or datatable,

How to search the records in data-table,in my application I am having plenty of records.
For eg: After the searching completes I am getting 50 records out of 1000, from the table, now I want to do the first, next, previous and last record navigation for this searched records only. how can I do this, I have done for normal navigation(directly connecting to sql) but getting some difficulty in showing navigation in searched records.
try this
var results = from myRow in myDataTable.AsEnumerable()
where myRow.Field<int>("RowNo") == 1
select myRow;
Here i assume that you have fetched data into Datatable using "fetchData()" method.
// Global variable
DataTable dt_rec = new DataTable();
int rowCount = 1;
int lastRowNum = 0;
In the form_load event, you called the "fetchData()" method. After then you click on "Next" button click event which look like this.
private void NextButton_Click(System.Object sender, System.EventArgs e)
{
if (lastRowNum > rowCount) {
rowCount = rowCount + 1;
setControlValue(dt_rec.Rows(rowCount - 1));
} else {
Interaction.MsgBox("Record Not Found", "Test");
}
}
And here is the "Previous" button click event.
private void Previous_Click(System.Object sender, System.EventArgs e)
{
if (rowCount > 1) {
rowCount = rowCount - 1;
setControlValue(dt_rec.Rows(rowCount - 1));
} else {
Interaction.MsgBox("Record Not Found", "Test");
}
}
And here is the click event of "First" button.
private void FirstButton_Click(System.Object sender, System.EventArgs e)
{
if (rowCount != 1) {
rowCount = 1;
setControlValue(dt_rec.Rows(0));
} else {
Interaction.MsgBox("Record Not Found", "Test");
}
}
And finally the "Last" click event.
private void LastButton_Click(System.Object sender, System.EventArgs e)
{
if (rowCount < lastRowNum) {
rowCount = lastRowNum;
setControlValue(dt_rec.Rows(lastRowNum - 1));
} else {
Interaction.MsgBox("Record Not Found", "Test");
}
}
Here you find the setControlValue() method which will set the control value. For exa.
public void setControlValue(DataRow dr)
{
if (!string.IsNullOrEmpty(dr["UserId"])) {
txtUserName.Text = dr["UserId"].ToString();
}
if (!string.IsNullOrEmpty(dr["WebUrl"])) {
txtWebUrl.Text = dr["WebUrl"].ToString();
}
// and so on.....
}
Hope all these thing might help you...

Pagination not working in SPGridView in Visual Web Part

Paging is not working when I click on pages. Nothing displays. Here is the code,
Markup on my ascx form,
<div class="mGrid">
<SharePoint:SPGridView
runat="server"
ID="gdSharedReport"
AutoGenerateColumns="false"
CssClass="mGrid"
AllowPaging="true"
PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt"
EmptyDataText="No Survey found."
OnSorting="gdSharedReport_Sorting"
OnPageIndexChanging="gdSharedReport_PageIndexChanging"
/>
</div>
Code on my ascx.cs,
public DataView dv;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GenerateGrid();
}
}
private DataTable BindData()
{
DataTable table;
table = new DataTable();
table.Columns.Add("SurveySet", typeof(string));
table.Columns.Add("SurveySection", typeof(string));
table.Columns.Add("SurveyQuestion", typeof(string));
table.Columns.Add("Employee", typeof(string));
table.Columns.Add("Supervisor", typeof(string));
table.Columns.Add("EmployeeNumber", typeof(string));
table.Columns.Add("SurveyDate", typeof(string));
DataRow row;
try
{
SPListItemCollection collListItems = list.GetItems(oQuery);
row = table.NewRow();
//Logic here to set rows
table.Rows.Add(row);
}
catch(Exception ex){}
return table;
}
private void GenerateGrid()
{
DataTable dt = BindData();
dv = new DataView(dt);
gdSharedReport.DataSource = dv;
gdSharedReport.AutoGenerateColumns = false;
gdSharedReport.AllowSorting = true;
gdSharedReport.Sorting += new GridViewSortEventHandler(gdSharedReport_Sorting);
//Setting bound fields here
gdSharedReport.PageSize = 10;
gdSharedReport.AllowPaging = true;
gdSharedReport.PageIndexChanging +=
new GridViewPageEventHandler(gdSharedReport_PageIndexChanging);
gdSharedReport.PagerTemplate = null;
if (ViewState["SortDirection"] != null && ViewState["SortExpression"] != null)
{
dv.Sort = ViewState["SortExpression"].ToString()
+ " " + ViewState["SortDirection"].ToString();
}
gdSharedReport.DataBind();
}
public void gdSharedReport_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gdSharedReport.PageIndex = e.NewPageIndex;
gdSharedReport.DataBind();
}
public void gdSharedReport_Sorting(object sender, GridViewSortEventArgs e)
{
string lastExpression = "";
if (ViewState["SortExpression"] != null)
lastExpression = ViewState["SortExpression"].ToString();
string lastDirection = "asc";
if (ViewState["SortDirection"] != null)
lastDirection = ViewState["SortDirection"].ToString();
string newDirection = "asc";
if (e.SortExpression == lastExpression)
newDirection = (lastDirection == "asc") ? "desc" : "asc";
ViewState["SortExpression"] = e.SortExpression;
ViewState["SortDirection"] = newDirection;
dv.Sort = e.SortExpression + " " + newDirection;
gdSharedReport.DataBind();
}
Here is how grid loads,
Here is what comes when I click to pages,
If I modify following function ,
public void gdSharedReport_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gdSharedReport.PageIndex = e.NewPageIndex;
gdSharedReport.DataBind();
}
To,
public void gdSharedReport_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gdSharedReport.PageIndex = e.NewPageIndex;
gdSharedReport.DataSource = BindData();
gdSharedReport.DataBind();
}
Then pagination start working but paging number disappears if I click on paging ,
You need to again specify pagination in page index change event.
public void gdSharedReport_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gdSharedReport.PageIndex = e.NewPageIndex;
gdSharedReport.AllowPaging = true;
gdSharedReport.PageIndexChanging += new GridViewPageEventHandler(gdSharedReport_PageIndexChanging);
gdSharedReport.PagerTemplate = null;
gdSharedReport.DataSource = BindData();
gdSharedReport.DataBind();
}

Janus 4 GridEx disable rows

I have have a Janus 4 GridEx control which includes a checkbox column.
I need to be able to disable certain rows (i.e make them non selectable/greyed out) depending the value of a particular column. The data for the grid is loaded from a database.
Any help would be appreciated.
You have to utilize the LoadingRow and SelectionChanged events of the Janus Grid.
This is a sample code: ( Here I'm checking the value of a particular column to be divided by 2)
private void grdEx_LoadingRow(object sender, Janus.Windows.GridEX.RowLoadEventArgs e)
{
if (Convert.ToInt32(e.Row.Cells["ID"].Value) % 2 == 0)
{
e.Row.RowStyle = new GridEXFormatStyle(e.Row.Table.RowFormatStyle);
e.Row.RowStyle.BackColor = Color.Gray;
}
}
private void grdEx_SelectionChanged(object sender, EventArgs e)
{
if (Convert.ToInt32(grdEx.GetValue("ID")) % 2 == 0)
{
if (grdEx.Row >= 0)
{
if (grdEx.Row == grdEx.RowCount - 1)
grdEx.Row = grdEx.Row - 1;
else
grdEx.Row = grdEx.Row + 1;
}
}
}
Depending on the Checkbox column , just see the sample code:
private void grdEX1_FormattingRow(object sender, RowLoadEventArgs e)
{
if (e.Row.RowIndex > -1 && e.Row.RowIndex < grdEX1.RowCount)
{
for (int i = 0; i < grdEX1.RootTable.Columns.Count; i++)
{
if (!Convert.ToBoolean(e.Row.Cells["checkboxCol"].Value))//checked So editable
{
e.Row.Cells[i].FormatStyle = new GridEXFormatStyle() { BackColor = Color.LightGray };
}
else
{
e.Row.Cells[i].FormatStyle = null;
}
}
}
}
To prevent the editing if the row is not checked :
private void grdEX1_EditingCell(object sender, EditingCellEventArgs e)
{
if(!Convert.ToBoolean(grdEX1.GetValue("checkboxCol"))) //not checked
{
e.Cancel = true;
return;
}
}

Error in DataGrid random behavior when scrolling in Silverlight 4.0

I have a datagrid and its rows are colored based on certain condtions..There is a vertical scrollbar for my datagrid.. If the user scrolls the datagrid the index of the colored rows r getting changed.. For eg: if 2 row is colored if the user scrolls down and comes up the index of the colored row is getting messed up...
here is the code....
dggeneralconfiguration.LoadingRow += new EventHandler<DataGridRowEventArgs>(grid1_LoadingRow);
dggeneralconfiguration.UnloadingRow += new EventHandler<DataGridRowEventArgs>(grid1_UnloadingRow);
void grid1_LoadingRow(object sender, DataGridRowEventArgs e)
{
ShowGeneralGrid c = e.Row.DataContext as ShowGeneralGrid;
if (c.Status == false)
{
if (e.Row != null)
{
e.Row.Background = new SolidColorBrush(Colors.Red);
//e.Row.Background = new SolidColorBrush(Colors.Transparent);
}
}
}
void grid1_UnloadingRow(object sender, DataGridRowEventArgs e)
{
ShowGeneralGrid c = e.Row.DataContext as ShowGeneralGrid;
if (c.Status == false)
{
if (e.Row != null)
{
e.Row.Background = new SolidColorBrush(Colors.Red);
//e.Row.Background = new SolidColorBrush(Colors.Transparent);
}
}
}
Better late than never. ;-) . I too had similar kind of problem.. try assigning row background to null on unloading row event.
private void dg_UnloadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Background = null;
}