getting error while Inserting data while using RadControls - webforms

Error:
Unable to update the EntitySet 'ClientFeedBack' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation.
Code:
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
ProTrakEntities1 objEntity = new ProTrakEntities1();
TextBox txtTitle = DetailsView1.FindControl("txtTask") as TextBox;
RadComboBox cmbStatus = DetailsView1.FindControl("cmbStatus") as RadComboBox;
RadComboBox cmbTaskType = DetailsView1.FindControl("cmbTasktype") as RadComboBox;
RadComboBox cmbTaskPriorty = DetailsView1.FindControl("cmbPriority") as RadComboBox;
string Description = (DetailsView1.FindControl("RadEditor1") as RadEditor).Content;
ClientFeedBack objResource = new ClientFeedBack();
objResource.Title = txtTitle.Text;
objResource.Description = Description;
objResource.TaskPriorityID = Convert.ToInt32(cmbTaskPriorty.SelectedValue);
objResource.TaskTypeID = Convert.ToInt32(cmbTaskType.SelectedValue);
objEntity.AddToClientFeedBacks(objResource);
objEntity.SaveChanges();
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CloseAndRebind('navigateToInserted');", true);
}
My table name is ClientFeedBack.

Check for the primary key, check if you table ClientFeedBack has primary key defined or not.

Related

Delete a row from Telerik Radgrid. How I get Index of that selected Row?

Here is the code I am using for delete functionality.
In RadgridItemdatabound funtion, I have to include this...
foreach (GridColumn col in RadGrid1.MasterTableView.Columns)
{
dataItem["TemplateDeleteColumn"].Attributes.Add("onclick","CellClick('" + dataItem.ItemIndex + "','" + col.UniqueName + "');");
}
Then I have to create Itemcommand function.
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "DeleteSelected")
{
GridDataItem item = (GridDataItem)e.Item;
var itemIndex = item.ItemIndex;
string LoginId = item.GetDataKeyValue("LoginId").ToString();
Int32 CampusCode = Convert.ToInt32(item.GetDataKeyValue("CampusCode"));
Definations def = new Definations();
Int32 Result = def.deleteUserAssignCampus(LoginId, CampusCode);
if (Result == 1)
{
BindDeptDatasimple();
cmbColumName.SelectedValue = "";
cmbDirection.SelectedValue = "";
Response.Redirect("UserCampus.aspx", false);
Session["deleteUserCampus"] = "Campus dissociated successfully.";
}
}
}
I could not get index of the selected row in the "var ItemIndex".
It always return zero index in ItemIndex. That's why the first row from the grid gets deleted. How I can get selected Index of selected row?
The item index will default to 0 because in your case you raise custom command
through MasterTableView's client object method.
You will have to make sure that in your custom JS function, CellClick, item index is passed into fireCommand client method via the 2nd argument.
masterTable.fireCommand("DeleteSelected", itemIndex);
Then in RadGrid1_ItemCommand event handler, you can retrieve the index value like this
GridDataItem item = (GridDataItem)e.Item;
var itemIndex = e.CommandArgument;

Bind List to GridviewComboboxcolumn in telerik radgrindview (winform)

I have a generic List like
List<return> returnlist
class return
{
public string returnid {get; set;
...
public List<string> Vouchernumbers
}
I bind the returnlist to the telerik radgridview.
How can i bind the voucherlist to the GridviewComboboxcolumn for each row ?
I have bind the voucherlist to the combobox after radgridview_complete_binding.
You need to create the grid with columns and data
You need to add the combobox column, initialize it.. Please check you need to have a dataeditor in here
Assign the string to datasource
comboColumn.DataSource = new String[] { "Test1", "Test2"};
You can bind the collections too:
Binding list BindingList<ComboBoxDataSourceObject> list = new BindingList<ComboBoxDataSourceObject>();
ComboBoxDataSourceObject object1 = new ComboBoxDataSourceObject();
object1.Id = 1;
object1.MyString = "Test 1";
list.Add(object1);
ComboBoxDataSourceObject object2 = new ComboBoxDataSourceObject();
object2.Id = 2;
object2.MyString = "Test 2";
list.Add(object2);
colboCol2.DataSource = list;
radGridView1.Columns.Add(colboCol2);
create radcombobox and set datasource and add it to rad grid
eg :
GridViewComboBoxColumn col = new GridViewComboBoxColumn();
col.DataSource = DAL.ActiveDb.GetList<SalesRep>().ToList().OrderBy(x => x.RepName).Select(x => new { Id = x.Id, x.RepName });
col.DropDownStyle = RadDropDownStyle.DropDown;
col.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
col.DisplayMember = "RepName";
col.ValueMember = "Id";
col.FieldName = "RepId";
col.HeaderText = "Rep Name";
col.Width = 200;
//var t = gridColInfo.Where(x => x.ColumnName.ToLower() == "repid").FirstOrDefault();
//if (t != null)
//{
// col.Width = t.ColumnWidth;
//}
this.radGridBillwiseOpening.Columns.Add(col);

Error trying to loop through Linq query results

I need to pull any amount of records that correspond to a specific value (CourseCode), and insert these records into another table. This code works fine as long as the Linq code returns only one record, however if there is any more than that I get the following message:
An object with the same key already exists in the ObjectStateManager.
The existing object is in the Modified state. An object can only be
added to the ObjectStateManager again if it is in the added.
Below is my code:
if (_db == null) _db = new AgentResourcesEntities();
var prodCodes = from records in _db.CourseToProduct
where records.CourseCode == course.CourseCode
select records;
foreach (var pt in prodCodes.ToList())
{
agentProdTraining.SymNumber = symNumber;
agentProdTraining.CourseCode = course.CourseCode;
agentProdTraining.ProductCode = pt.ProductCode;
agentProdTraining.DateTaken = course.DateTaken;
agentProdTraining.Method = course.Method;
agentProdTraining.LastChangeOperator = requestor;
agentProdTraining.LastChangeDate = DateTime.Now;
agentProdTraining.DateExpired = course.ExpirationDate;
agentProdTraining.ProductCode = pt.ProductCode;
agentProdTraining.NoteId = pt.NoteId;
_db.AgentProductTraining.AddObject(agentProdTraining);
_db.SaveChanges();
PtAdded++;
EventLog.WriteEntry(sSource, "Product Training added", EventLogEntryType.Warning);
}
The loop is re-adding the same agentProdTraining object even though property values are changed. Create a new instance for each loop execution.
foreach (var pt in prodCodes.ToList())
{
var agentProdTraining = new AgentProductTraining();
agentProdTraining.SymNumber = symNumber;
agentProdTraining.CourseCode = course.CourseCode;
agentProdTraining.ProductCode = pt.ProductCode;
agentProdTraining.DateTaken = course.DateTaken;
agentProdTraining.Method = course.Method;
agentProdTraining.LastChangeOperator = requestor;
agentProdTraining.LastChangeDate = DateTime.Now;
agentProdTraining.DateExpired = course.ExpirationDate;
agentProdTraining.ProductCode = pt.ProductCode;
agentProdTraining.NoteId = pt.NoteId;
_db.AgentProductTraining.AddObject(agentProdTraining);
_db.SaveChanges();
PtAdded++;
EventLog.WriteEntry(sSource, "Product Training added", EventLogEntryType.Warning);
}

How to bind listbox items using observable colletion in wp7?

My Code:
ObservableCollection<SampleCheckedData> interestrates = new ObservableCollection<SampleCheckedData>();
XDocument xmlDocu = XDocument.Load(new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(result)));
interestrates = (from rts in xmlDocu.Descendants("Friend")
select new SampleCheckedData
{
Id = (string)rts.Element("userid"),
Name = (string)rts.Element("name"),
Icon = (string)rts.Element("imageurl"),
VisibleStatus = (string)rts.Element("visiblestatus"),
AppStatus = (string)rts.Element("loginstatus"),
imgBubble =bitmapRed,
}).ToList<SampleCheckedData>();
Then Getting Error as can't implicitly convert system.collection.generic.list to system.collection.observablecollection like that.How to bind listbox items using observable collection?
EDIT:
Button b = sender as Button;
var res = interestrates.Where(a => a.Id.Equals(((System.Windows.FrameworkElement)(e.OriginalSource)).Tag)).ToList();
if (res.Count == 1)
interestrates.Remove(res.First());
interestrates = new ObservableCollection<SampleCheckedData>();
lstFriendRequuest.ItemsSource = "";
bindGetFriends();
Here successfully deleting item from list but after calling bindGetFriends() in that binding the items newly then i am not getting new items getting old items.why the service returning old items list?
Use this extension:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
public static class Extensions
{
public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> collection)
{
var observableCollection = new ObservableCollection<T>();
foreach (var item in collection) observableCollection.Add(item);
return observableCollection;
}
}
Usage:
interestrates = (from rts in xmlDocu.Descendants("Friend")
select new SampleCheckedData
{
Id = (string)rts.Element("userid"),
Name = (string)rts.Element("name"),
Icon = (string)rts.Element("imageurl"),
VisibleStatus = (string)rts.Element("visiblestatus"),
AppStatus = (string)rts.Element("loginstatus"),
imgBubble =bitmapRed,
}).ToObservableCollection<SampleCheckedData>();
Change your Observable collection to List,
List<SampleCheckedData> interestrates = new List<SampleCheckedData>();
You can also bind List to ListBox, instead of ObservableCollection
And to solve your other problem of deleting selected item from listbox, try the following code:
var selectedIndex = listbox.SelectedIndex;
var listItems = listbox.ItemsSource as List<SampleCheckedData>;
listItems.RemoveAt(selectedIndex);
listbox.ItemsSource = null;
listbox.ItemsSource = listItems;
If still you are facing problems, let me know

when the gridview is set to display values from session arraylist "array" the following error is shown "Object must implement IConvertible." in c#

when the user selects "add to cart" the itemID of that row item is added to arraylist which is then stored in a session.
in the shopping cart page ive got a gridview which reads name, price from item db where session["array"]=itemID
the error is displayed when the shopping cart page loads.
all i want to do is to get the list of items the user has selected which is in the arraylist and populate them in the gridview in the shopping cart page.
public partial class Drama_k : System.Web.UI.Page
{
ArrayList array;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["array"] == null)
{
array = new ArrayList();
Session.Add("array", array);
}
else
array = Session["array"] as ArrayList;
GridView1.DataSource = array;
GridView1.DataBind(); //Edit 2
}
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddToCart")
{
int index = Convert.ToInt32(e.CommandArgument);
array.Add(GridView2.DataKeys[index].Value.ToString());
}
}
}
shopping cart page only has a gridview where the sql statement is
SELECT [ItemID], [Name], [RelDate], [Price], [Status] FROM [item_k] WHERE ([ItemID] = #ItemID)
value of ([ItemID] = #ItemID) is Session("array")
what seems to be the issue here?
thanks in advance.
//edit 2
ArrayList tmpArrayList = new ArrayList();
tmpArrayList = (ArrayList)Session["array"];
string itemIDs = string.Empty;
foreach (object itemID in tmpArrayList)
{
itemIDs += itemID.ToString() + ',';
}
itemIDs = itemIDs.Substring(0, itemIDs.Length - 1);
where itemIDs now hold values "3,16"
I placed the query string in the query builder window and i get this error
when i run the website.
"Invalid column name ' + itemIDs + '. "
It looks like you're trying to assign an ArrayList as a parameter to your query, which isn't possible. You'll have to assemble a string that contains a comma-separated list of the ArrayList's contents and use that to build the query, rather than passing it in as a parameter. Note that this will require changing the query string as well, using WHERE...IN instead of WHERE...=.
string itemIDs = string.Empty;
foreach (int itemID in array)
{
itemIDs += itemID.ToString() + ',';
}
itemIDs = itemIDs.Substring(0, itemIDs.Length - 1); // remove last comma
string query = "SELECT [ItemID], [Name], [RelDate], [Price], [Status] FROM [item_k] WHERE [ItemID] IN (" + itemIDs + ")"

Resources