Removing an Object from a DataSource - asp.net

I am trying to remove an item which is selected in a datagrid from the domaindatasource that it uses, through a button click.
The code for the button click is:
private void medItemRemove_Click(object sender, RoutedEventArgs e)
{
if (medicineInventoryDataGrid.SelectedIndex != -1)
{
MedicineInventory M = (MedicineInventory)medicineInventoryDataGrid.SelectedItem;
MedicineInventory toRemove = (from a in ctx.MedicineInventories where (a.MedicineInventoryId == M.MedicineInventoryId) select a).Single();
ctx.MedicineInventories.Remove(toRemove);
}
}
However on clicking, I get the following error:
"The Specific Entity is not contained in the EntitySet."
Where am I going wrong?

try
MedicineInventory M = (MedicineInventory)medicineInventoryDataGrid.SelectedItem.DataItem;

Related

Disable/Enable save button based on the mandatory field being null or empty using Behaviors

I created a behavior 'RequiredValidationBehavior' and applied to one of the entry fields in XAML page.
It works as, if the entry field is empty, the placeholder color becomes red, thus indicating the mandatory field. This works fine.
The issue I am facing is with the button on the page, where it should to be disabled if this entry field is empty and enabled if the entry field has some value.
I want to achieve this using the behavior I created.
Thanks for the responses in advance.
You can try with below code:
public void entryTextChanged(object obj, EventArgs args)
{
if (entry.Text.Length > 0)
button.IsEnable= true;
else
button.IsEnable = false;
}
And just assign this event to Entry on "TextChanged" Event.
private void EntryMessage(object sender, EventArgs e)
{
var keyword = Message.Text;//your entry
if (string.IsNullOrEmpty(keyword))
{
OnAddBT.IsEnabled = false;//your button
}
else
{
OnAddBT.IsEnabled = true;//your button
}
}

updating view from gridview

My query is
SELECT
deldate,
transno,
matno,
MAT_NAME,
rawpkgno,
MAX(SWITCH(deldate=? ,ORDCASES )) AS [CURRDATE],
MAX(SWITCH(deldate=? ,ORDCASES )) AS [previous_day],
MAX(SWITCH(deldate=? ,ORDCASES )) AS [Last_date]
FROM
invorder
WHERE
invorder.strno =54009
OR [invorder.deldate] IS NULL
GROUP BY
matno,
MAT_NAME,
rawpkgno,
transno,
deldate
I want to edit this from gridview. As this is view I cannot edit it from gridview. But is their any way through which previous_day, Last_date columns can be constant and I can only make changes to CURRDATE column. Kindly help me.
Hi you have to bind the row_added event on grid and there make the all the cell read only except u want editable.. check the below code..
public Form1()
{
InitializeComponent();
dataGridView1.RowsAdded += new DataGridViewRowsAddedEventHandler(dataGridView1_RowsAdded);
}
void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
foreach (DataGridViewCell item in dataGridView1.Rows[e.RowIndex].Cells)
{
// here i used the 5 because i want to make the 5th index cell as editable.
if (item.ColumnIndex == 5)
{
continue;
}
item.ReadOnly = true;
}
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = bind_your_data_source_here;
dataGridView1.Refresh();
}
you can find the sample from here..
Download

Create a newEventHandler for a button. But Data fields are empty for some reason

I create a new event Handler to handle two different events. One is for saving a new document. The other is for saving an edit.
I added this in my Page_load:
if (Request.QueryString["ExhibitID"] != null)//new
{
if (!IsPostBack)
{
ddlCaseFiles.DataSourceID = "dsCaseFiles";
ddlCaseFiles.DataTextField = "Display";
ddlCaseFiles.DataValueField = "FileID";
rbByFileID.Checked = true;
rbMyFiles.Checked = false;
ddlCaseFiles.DataBind();
editExhibit(int.Parse(Request.QueryString["ExhibitID"]));//new
exhibitHeader.InnerText = "Edit Exhibit";
}
hidSavedExhibitID.Value = Request.QueryString["ExhibitID"];
saveExhibitBtn.Click += new EventHandler(this.btnUpdateExhibit_Click);
}
else
{
saveExhibitBtn.Click += new EventHandler(this.saveExhibitBtn_Click);
}
my save method for some reason keeps looping then crashing because the second time it goes through, there is no data since I reset it after the first save. I have no idea why it is running my save method twice.
this is my save method :
protected void saveExhibitBtn_Click(object sender, EventArgs e)
{
hidSavedExhibitID.Value = null;
int newExhibitID = saveExhibit();
int propertyID = autoCreateProperty(newExhibitID);
linkExhibitAndProperty(newExhibitID, propertyID);
SaveInfoIntoSessionVariables();
ClearFormFields();
}
the "saveExhibit()" method is where I actually access the DB and store everything. It works fine.
Because you re bind your datas in your Page_Load.
You must persist your datas with ViewState, EnableViewState="true"
You bind your datas just one time, in the ! IsPostBack. in order to not erase the selected values
If(! IsPostBack)
{
//Bind your datas with `DataBind()`
}

Can't submit changes to database or page controls

I've got a pretty simple page, consisting of two DropDownLists populated from the database, and a button. The purpose of the page is pretty simply to allow users to delete an entry from the database. When the button is clicked then a simple LINQ query is executed to delete the intended target, and remove the entry from the dropdownlists, but it doesn't work unless the response is redirected within that function, even if SubmitChanges() was called. Why would this happen?
Edit: Code
protected void Page_Init(object sender, EventArgs e)
{
var result = Database.DB.Data.GetTable<Database.tbl_module_>().Where(module => module.deptCode == ((User)Session["user"]).deptCode);
foreach (var row in result)
{
this.listModuleCode.Items.Add(new System.Web.UI.WebControls.ListItem(row.code));
this.listModuleTitle.Items.Add(new System.Web.UI.WebControls.ListItem(row.title));
}
}
protected void Delete_Click(object sender, EventArgs e)
{
var DB = Database.DB.Data;
var table = DB.GetTable<Database.tbl_module_>();
var result = table.Where(module => module.deptCode == ((User)Session["user"]).deptCode && module.code == listModuleCode.SelectedItem.Text);
listModuleCode.Items.Remove(listModuleCode.SelectedItem);
listModuleTitle.Items.Remove(listModuleTitle.SelectedItem);
table.DeleteAllOnSubmit(result);
DB.SubmitChanges();
Response.Redirect("deletemodule.aspx"); // redirect to this page
}
We need to see your code to help more probably. However:
You need to make sure it knows to delete on submit:
var q = db.Customers.Where(c => c.CustomerID == 2).Single();
db.Customers.DeleteOnSubmit(q);
db.SubmitChanges();
Don't forget you can pass straight SQL to the object:
db.ExecuteCommand("DELETE FROM Customers WHERE ID = 2");
Which you might think is easier.

Highligh a cell in a gridview

i have a dropdownlist with two values status- 'pending' and 'completed'. while im entering a new task my status is 'pending' once i finish it off i ll change my status as 'completed'. i have displayed it in gridview. the cell which i update as 'completed' have to be highlighted and the remaining cells in the status column i.e 'pending' has to be in another color
If a serverside callback is possible in this scenario, then subscribe the OnRowDataBound-Event and look for the specific row and set the css class of a label to something different. You could use a TemplateColumn with a Label in it.
E.g.
protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == RowType.DataRow)
{
YourObject _item = (YourObject)e.Row.DataItem;
Literal _litFromTemplate = (Literal)e.Row.FindControl("litFromTemplate");
if(_item.Equals(anotherItem)) // or check for any other condition, like _item.Foo == 123
{
_litFromTemplate.CssClass = 'highlightingMe';
}
else
{
_litFromTemplate.CssClass = 'normalcssclass';
}
}
}

Resources