To retrieve the value from session and assign it to a variable - asp.net

In the below code i get all the ids in a arraylist and store it in a session in sample.aspx and retrieve the session value in test.aspx.Now i want to assign the project id to DataSet dsField in page load .How can i get that value separately.
sample.aspx
Button btnView = (Button)e.CommandSource;
Label lblProjectId = (Label)btnView.Parent.FindControl("ProjectID");
Label lblBatchID = (Label)btnView.Parent.FindControl("BatchID");
Label lblImageID = (Label)btnView.Parent.FindControl("ImageID");
Label lblReasons = (Label)btnView.Parent.FindControl("Reasons");
Label lblLayerID = (Label)btnView.Parent.FindControl("LayerID");
Label lblStatusID = (Label)btnView.Parent.FindControl("StatusID");
Label lblProcessID = (Label)btnView.Parent.FindControl("ProcessID");
ArrayList SearchUrlValues = new ArrayList();
SearchUrlValues.Add(lblProjectId);
SearchUrlValues.Add(lblBatchID);
SearchUrlValues.Add(lblProjectId);
SearchUrlValues.Add(lblImageID);
SearchUrlValues.Add(lblReasons);
SearchUrlValues.Add(lblLayerID);
SearchUrlValues.Add(lblStatusID);
SearchUrlValues.Add(lblProcessID);
Session["ProjectDetails"] = SearchUrlValues.ToArray();
Response.Write(SearchUrlValues);
test.aspx:
Array SearchUrlValues = (Array)Session["ProjectDetails"];
if (!IsPostBack)
{
DataSet dsField = GetFieldData(10);//how to assign projectid instead of 10
gmasFieldsContr.dtFieldsInfo = dsField.Tables[0];
gmasFieldsContr.EnumTable = dsField.Tables[1];
gmasFieldsContr.RegularExpressionTable = dsField.Tables[3];
gmasFieldsContr.BindData();
}
public DataSet GetFieldData(int iProjectID)
{
try
{
SqlParameter[] SqlParam = new SqlParameter[1];
SqlParam[0] = new SqlParameter("#i_ProjectID", SqlDbType.Int);
SqlParam[0].Value = iProjectID;
return ExecuteQuery(SqlParam, "spGetFieldData");
}
catch (Exception ex)
{
throw new Exception(ex.Message.ToString());
}
}

Edited
In Sample.aspx don't store SearchUrlValues as Array
Button btnView = (Button)e.CommandSource;
Label lblProjectId = (Label)btnView.Parent.FindControl("ProjectID");
Label lblBatchID = (Label)btnView.Parent.FindControl("BatchID");
Label lblImageID = (Label)btnView.Parent.FindControl("ImageID");
Label lblReasons = (Label)btnView.Parent.FindControl("Reasons");
Label lblLayerID = (Label)btnView.Parent.FindControl("LayerID");
Label lblStatusID = (Label)btnView.Parent.FindControl("StatusID");
Label lblProcessID = (Label)btnView.Parent.FindControl("ProcessID");
ArrayList SearchUrlValues = new ArrayList();
SearchUrlValues.Add(lblProjectId);
SearchUrlValues.Add(lblBatchID);
SearchUrlValues.Add(lblProjectId);
SearchUrlValues.Add(lblImageID);
SearchUrlValues.Add(lblReasons);
SearchUrlValues.Add(lblLayerID);
SearchUrlValues.Add(lblStatusID);
SearchUrlValues.Add(lblProcessID);
Session["ProjectDetails"] = SearchUrlValues; // Store it as ArrayList
Response.Write(SearchUrlValues);
Then test.aspx, convert Session object to ArrayList;
var SearchUrlValues = (ArrayList)Session["ProjectDetails"];
if (!IsPostBack)
{
var projectId = int.Parse(SearchUrlValues[0].ToString());
DataSet dsField = GetFieldData(projectId);//how to assign projectid instead of 10
gmasFieldsContr.dtFieldsInfo = dsField.Tables[0];
gmasFieldsContr.EnumTable = dsField.Tables[1];
gmasFieldsContr.RegularExpressionTable = dsField.Tables[3];
gmasFieldsContr.BindData();
}
By the way, please note that you're adding lblProjectId twice;
SearchUrlValues.Add(lblProjectId); // First
SearchUrlValues.Add(lblBatchID);
SearchUrlValues.Add(lblProjectId); // Second
Additionally, I would prefer to use an object to store these values in the session.
public class SearchUrlValues
{
public int lblProjectId { get; set; }
public int lblBatchID { get; set; }
public int lblImageID { get; set; }
public int lblReasons { get; set; }
public int lblLayerID { get; set; }
public int lblStatusID { get; set; }
public int lblProcessID { get; set; }
}
Then, instead of arraylist;
var newSearchUrlValues = new SearchUrlValues()
{
lblProjectId = lblProjectId,
lblBatchID = lblBatchID,
lblImageID = lblImageID,
lblReasons = lblReasons,
lblLayerID = lblLayerID,
lblStatusID = lblStatusID,
lblProcessID = lblProcessID
};
Session["ProjectDetails"] = newSearchUrlValues;
And retrieve it like;
var searchUrlValues = (SearchUrlValues)Session["ProjectDetails"];
var projectId = searchUrlValues.lblProjectId;

Try Like This
ArrayList SearchUrlValues = (ArrayList)Session["ProjectDetails"];
if (!IsPostBack)
{
DataSet dsField = GetFieldData(Convert.ToInt32(SearchUrlValues[0].ToString()));
//fetech 1st element of array List
gmasFieldsContr.dtFieldsInfo = dsField.Tables[0];
gmasFieldsContr.EnumTable = dsField.Tables[1];
gmasFieldsContr.RegularExpressionTable = dsField.Tables[3];
gmasFieldsContr.BindData();
}
public DataSet GetFieldData(int iProjectID)
{
try
{
SqlParameter[] SqlParam = new SqlParameter[1];
SqlParam[0] = new SqlParameter("#i_ProjectID", SqlDbType.Int);
SqlParam[0].Value = iProjectID;
return ExecuteQuery(SqlParam, "spGetFieldData");
}
catch (Exception ex)
{
throw new Exception(ex.Message.ToString());
}
}

Related

how to Add list of products from cart?

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);
}

customize the height and width of ESRI map callout

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);
}
}

Gridview data binding

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();

How to read id with loop from list in asp.net

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);
}

Editable gridview based on list

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();
}

Resources