dears,
i have an API working with ASP.Net Core 3.1 posting orders
i want to post order head and get all items from another api in cart items and post it in order items my code as below
[HttpPost("addOrderHead")]
public async Task<ActionResult<OrderDto>> Posting(OrderDto dto)
{
try
{
if (dto == null)
{
return BadRequest(ModelState);
}
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var mappedEntities = _mapper.Map<Order>(dto);
_orderRepository.Add(mappedEntities);
if (await _orderRepository.Save())
{
int id = mappedEntities.OrderID;
var cartItems = await _cartItemRepository.GetCartItems(mappedEntities.ApplicationUserId);
var cartDto = new CartItemDto();
foreach(var item in cartItems)
{
cartDto.ItemID = item.ItemID;
cartDto.ItemLookupCode = item.ItemLookupCode;
cartDto.CategoryID = item.CategoryID;
cartDto.DepartmentID = item.DepartmentID;
cartDto.itemDescription = item.itemDescription;
cartDto.SubDescription3 = item.SubDescription3;
cartDto.Quantity = item.Quantity;
cartDto.Weight = item.Weight;
cartDto.SnapShotPrice = item.SnapShotPrice;
cartDto.StoreId = item.StoreId;
cartDto.barcode = item.barcode;
cartDto.Email = item.Email;
cartDto.ItemImage = item.ItemImage;
};
var items = new OrderItems()
{
OrderId = id,
ItemID = cartDto.ItemID,
ItemLookupCode = cartDto.ItemLookupCode,
CategoryID = cartDto.CategoryID,
DepartmentID = cartDto.DepartmentID,
itemDescription = cartDto.itemDescription,
SubDescription3 = cartDto.SubDescription3,
Quantity = cartDto.Quantity,
Weight = cartDto.Weight,
SnapShotPrice = cartDto.SnapShotPrice,
StoreId = cartDto.StoreId,
barcode = cartDto.barcode,
Email = cartDto.Email,
ItemImage = cartDto.ItemImage,
};
_orderItemsRepository.Add(items);
await _orderItemsRepository.Save();
return Ok(id);
}
return BadRequest(ModelState);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.InnerException.Message);
}
}
every time i run this api order header added successfully and order items add first item only
which cart items return with array of items ,
can any one help me in that ,
You need to put Add (and maybe .Save()) inside the foreach:
var cartItems = await _cartItemRepository.GetCartItems(mappedEntities.ApplicationUserId);
foreach(var item in cartItems)
{
var cartDto = new CartItemDto(); // inside foreach
cartDto.ItemID = item.ItemID;
....
var item = new OrderItems() // one item - not: items
{
OrderId = id,
ItemID = cartDto.ItemID,
...
ItemImage = cartDto.ItemImage,
};
_orderItemsRepository.Add(item); // add item before moving to next item.
}
await _orderItemsRepository.Save();
BTW. I'm not sure why you need cartDto; I think you can eliminate it:
var cartItems = await _cartItemRepository.GetCartItems(mappedEntities.ApplicationUserId);
foreach(var item in cartItems)
{
var item = new OrderItems() // one item - not: items
{
OrderId = id,
ItemID = item.ItemID,
...
ItemImage = item.ItemImage,
};
_orderItemsRepository.Add(item); // add item before moving to next item.
}
await _orderItemsRepository.Save();
The code is not correct.In the below line you it should be List instead of object
var cartItems = await _cartItemRepository.GetCartItems(mappedEntities.ApplicationUserId);
var cartDtoList = new List<CartItemDto>();
foreach(var item in cartItems)
{
carDto carDto= new CarDto();
cartDto.ItemID = item.ItemID;
cartDto.ItemLookupCode = item.ItemLookupCode;
cartDto.CategoryID = item.CategoryID;
cartDto.DepartmentID = item.DepartmentID;
cartDto.itemDescription = item.itemDescription;
cartDto.SubDescription3 = item.SubDescription3;
cartDto.Quantity = item.Quantity;
cartDto.Weight = item.Weight;
cartDto.SnapShotPrice = item.SnapShotPrice;
cartDto.StoreId = item.StoreId;
cartDto.barcode = item.barcode;
cartDto.Email = item.Email;
cartDto.ItemImage = item.ItemImage;
carDtoList.Add(carDto)
};
Similarly the orderItems will also be list.
The other simplest solution is to put all the things inside foreach loop like this
foreach(var item in cartItems)
{
cartDto.ItemID = item.ItemID;
cartDto.ItemLookupCode = item.ItemLookupCode;
cartDto.CategoryID = item.CategoryID;
cartDto.DepartmentID = item.DepartmentID;
cartDto.itemDescription = item.itemDescription;
cartDto.SubDescription3 = item.SubDescription3;
cartDto.Quantity = item.Quantity;
cartDto.Weight = item.Weight;
cartDto.SnapShotPrice = item.SnapShotPrice;
cartDto.StoreId = item.StoreId;
cartDto.barcode = item.barcode;
cartDto.Email = item.Email;
cartDto.ItemImage = item.ItemImage;
var items = new OrderItems()
{
OrderId = id,
ItemID = cartDto.ItemID,
ItemLookupCode = cartDto.ItemLookupCode,
CategoryID = cartDto.CategoryID,
DepartmentID = cartDto.DepartmentID,
itemDescription = cartDto.itemDescription,
SubDescription3 = cartDto.SubDescription3,
Quantity = cartDto.Quantity,
Weight = cartDto.Weight,
SnapShotPrice = cartDto.SnapShotPrice,
StoreId = cartDto.StoreId,
barcode = cartDto.barcode,
Email = cartDto.Email,
ItemImage = cartDto.ItemImage,
};
_orderItemsRepository.Add(items);
await _orderItemsRepository.Save();
return Ok(id);
}
here is the formatted String which i am binding to callout but i am able to see only some contents of my string in callout
private async void WebMapView_GeoViewTapped(object sender, GeoViewInputEventArgs e)
{
try
{
WebMapView.DismissCallout();
MapPoint mapLocation = null;
var layers = await WebMapView.IdentifyLayersAsync(e.Position, 20, false);
if (layers.Count > 0)
{
foreach (var idResults in layers)
{
FeatureLayer idLayer = idResults.LayerContent as FeatureLayer;
await idLayer.LoadAsync();
var result = layers.First();
var feature = result.GeoElements.First() as ArcGISFeature;
await feature.LoadAsync(); // Load feature to get all attributes
Feature idFeature = result.GeoElements.First() as Feature;
featureAttrs = idFeature.Attributes;
var stateExtent = idFeature.Geometry;
Graphicoverlay = new GraphicsOverlay();
Graphic graphicLine = null;
graphicLine = new Graphic(stateExtent, AppConstant.HighLight1);
Graphicoverlay.Graphics.Add(graphicLine);
EnvelopeBuilder myEnvelopeBuilder = new EnvelopeBuilder(SpatialReferences.WebMercator);
//Geometry abd = oneGraphic.Geometry;
myEnvelopeBuilder.UnionOf(graphicLine.Geometry.Extent);
myEnvelopeBuilder.Expand(1.3);
mapLocation = myEnvelopeBuilder.Center;
Geometry myGeometry = GeometryEngine.Project(mapLocation, SpatialReferences.WebMercator);
MapPoint projectedLocation = (MapPoint)myGeometry;
string formattedString = "";
if (featureAttrs.Count > 0)
{
foreach (var attributes in featureAttrs)
{
string recordOneAttribute = $"{attributes.Key} {attributes.Value}";
formattedString = $"{formattedString}\n{ recordOneAttribute}";
}
}
CalloutDefinition calloutDef = new CalloutDefinition("Feature:",formattedString);
WebMapView.ShowCalloutAt(mapLocation, calloutDef);
}
}
I have a problem when when while loop repeats it iteration the already binded is replacing by new records but i want to bind all data by the iteration of while loop.
I am a new I have no idea how it can be.
while (i >= 0)
{
if (i != 0)
{
group_idd = group_ids[--i];
}
SqlConnection connection3 = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection_String"].ConnectionString);
using (connection3)
{
using (SqlCommand cmdd = new SqlCommand())
{
cmdd.CommandText = "SELECT [news_category],[id] FROM [news_profile] WHERE [user_id]='" + user_id + "' AND [group_id]='" + group_idd + "' AND [profile_id] IS NOT NULL";
cmdd.Connection = connection3;
connection3.Open();
GridView1.DataSource = cmdd.ExecuteReader();
GridView1.DataBind();
connection3.Close();
}
}
if (i == 0)
{
--i;
}
}
Create a small class
public class NewsProfile
{
public string NewsID { get; set; }
public string NewsCategory { get; set; }
}
Now in your code-behind, do this
List<NewsProfile> newsProfiles = new List<NewsProfile>();
// while loop here
SqlConnection connection3 = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection_String"].ConnectionString);
using (connection3)
{
using (SqlCommand cmdd = new SqlCommand())
{
cmdd.CommandText = "SELECT [news_category],[id] FROM [news_profile] WHERE [user_id]='" + user_id + "' AND [group_id]='" + group_idd + "' AND [profile_id] IS NOT NULL";
cmdd.Connection = connection3;
connection3.Open();
while (reader.Read())
{
NewsProfile np = new NewsProfile();
np.NewsCategory = reader.IsDBNull(0) ? "" : reader.GetString(0);
np.NewsID = reader.IsDBNull(1) ? "" : reader.GetString(1);
newsProfiles.Add(np);
}
}
}
//end while loop here
GridView1.DataSource = newsProfiles;
GridView1.DataBind();
here is my code:
public class ListData
{
public int Id { get; set; }
public int LinkedUserId { get; set; }
}
List<ListData> DataList = new List<LinkData>();
using (SqlConnection SqlConnections = new SqlConnection(Global.con))
using (SqlCommand SqlCommands = new SqlCommand("SELECT Id,LinkedUserID From Users", SqlConnections))
{
SqlConnections.Open();
using (SqlDataReader SqlDataReaders = SqlCommands.ExecuteReader(CommandBehavior.CloseConnection))
{
while (SqlDataReaders.Read())
{
ListData newItem = new ListData();
newItem.Id = SqlDataReaders.GetInt32(0);
newItem.LinkedUserId = SqlDataReaders.GetInt32(1);
DataList.Add(newItem);
}
SqlDataReaders.Close();
}
There is a ListData class there is a two Ids one is Id and sec is LinkedUserId. I want to read both id from ListData and return one by one from running loop. I want to do something like that below:
for (int i = 0; i < DataList .Count; i++)
{
string id = DataList[i].ToString();
}
Does anybody have any idea how can i return value form for loop or using foreach loop?
Thank you
Is it what you need?:
var result = new int[DataList.Count*2];
for (int i = 0; i < DataList.Count; i++)
{
result[i * 2] = DataList[i].Id;
result[i * 2 + 1] = DataList[i].LinkedUserId;
}
foreach (ListData listData in DataList)
{
string id = Convert.ToString(listData.Id);
string linkedUserId = Convert.ToString(listData.LinkedUserId);
}
Is it possible to create a gridview based on a list? I have the following list:
ID = 1
Name = John
Zip = 33141
ID = 2
Name = Tim
Zip = 33139
I want to be able to create an editable gridview with this list
When i bind it to the grid view, it seems to put everyting in one column, and i can't figure out how to get it to seperate it into different columns
Here is my code for setting the DataSource of the GridView:
DataTable table = ConvertListToDataTable(personList);
GridView1.DataSource = table;
GridView1.DataBind();
static DataTable ConvertListToDataTable(List<string> list)
{
// New table.
DataTable table = new DataTable();
// Get max columns.
int columns = 7;
// Add columns.
for (int i = 0; i < columns; i++)
{
table.Columns.Add();
}
// Add rows.
foreach (var rd in list)
{
table.Rows.Add(rd);
}
return table;
}
Here is an example:
private class Person
{
int m_iID;
string m_sName;
string m_sZip;
public int ID { get { return m_iID; } }
public string Name { get { return m_sName; } }
public string Zip { get { return m_sZip; } }
public Person(int iID, string sName, string sZip)
{
m_iID = iID;
m_sName = sName;
m_sZip = sZip;
}
}
private List<Person> m_People;
private void ConvertListToDataTable(List<Person> People)
{
DataTable table = new DataTable();
DataColumn col1 = new DataColumn("ID");
DataColumn col2 = new DataColumn("Name");
DataColumn col3 = new DataColumn("Zip");
col1.DataType = System.Type.GetType("System.String");
col2.DataType = System.Type.GetType("System.String");
col3.DataType = System.Type.GetType("System.String");
table.Columns.Add(col1);
table.Columns.Add(col2);
table.Columns.Add(col3);
foreach (Person person in People)
{
DataRow row = table.NewRow();
row[col1] = person.ID;
row[col2] = person.Name;
row[col3] = person.Zip;
table.Rows.Add(row);
}
GridView1.DataSource = table;
GridView1.DataBind();
}