JqGrid: Export to Excel - Object Reference not set to an instance - sorting

I have a select option value as
<select id="FilterOptions" role="ListBox">
<option value="7">1 Week</option>
<option value="14">2 Week</option>
<option value="28">4 Week</option>
<option value="90">2 Months</option>
<option value="182">6 Months</option>
<option value="365">All</option>
</select>
I have an Icon to export to excel and below is the functionality.
My JqGrid works fine with all the option. But here is the problem. After Selecting ALL and All records are loaded without any error. But after exporting to excel I receive "Object Reference not set to an instance on the line specified below"
How can I solve this error?
public string ExportReport(int cp, int noOfOrders)
{
string conndb=ConfigurationManager.AppSettings["Db"];
const string Ids ="0";
Company pList = _items.GetByCID(cp);
IQueryable<Product> List
List=product.SQLDatabase().GetListByCompandIds(cp, Convert.ToInt32(noOfOrders), conndb, Ids)
XLWorkbook workbook = new XLWorkbook(XLEventTracking.Disabled);
IXLWorksheet worksheet = workbook.Worksheets.Add("ExportReport");
worksheet.Cell(1, 1).Value = "Received";
worksheet.Cell(1, 2).Value = "Customer Email";
worksheet.Cell(1, 3).Value = "First Name";
worksheet.Cell(1, 4).Value = "Last Name";
worksheet.Cell(1, 5).Value = "Company Name";
worksheet.Cell(1, 6).Value = "Product Name";
int i = 2;
foreach (EReport ExpReport in List)
{
worksheet.Cell(i, 1).SetValue(ExpReport.received);
worksheet.Cell(i, 2).SetValue(ExpReport.Email);
worksheet.Cell(i, 3).SetValue(ExpReport.FirstName); // *Here is the line where I am receiving an error*
worksheet.Cell(i, 4).SetValue(ExpReport.LastName);
worksheet.Cell(i, 5).SetValue(ExpReport.CompanyName);
worksheet.Cell(i, 6).SetValue(ExpReport.ProductName);
++i;
}
worksheet.Columns().AdjustToContents();
Session["Workbook"] = workbook;
string filename;
filename = "Product-Report";
return filename;
}
How to export with sorting with a specific column received?

Related

Why enter laravel loop data max 40 row?

I created a store function on my controller. At the time I will enter data with just one submit, only indexed up to 40 data will be entered. While those 40 and above do not enter the database. Why did this happen? What's the solution? Please i need help.
Sorry for my bad English.
Thanks
I am use Laravel 5.6
public function store(Request $request)
{
//
foreach($request->quantity as $quantity){
if($quantity == NULL){
}
if($quantity != NULL){
$data = new KomponenOlahan;
$data->quantity = $quantity;
$harga_satuan_mu = Input::get('harga_satuan_mu');
$total = $quantity*$harga_satuan_mu;
$data->kode_proyek = Input::get('kode_proyek');
$data->nama_proyek = Input::get('nama_proyek');
$data->kode_panel = Input::get('kode_panel');
$data->nama_panel = Input::get('nama_panel');
$data->schedule_kirim = Input::get('schedule_kirim');
$data->nama_sales = Input::get('nama_sales');
$data->ukuran_panel = Input::get('ukuran_panel');
$data->ref = Input::get('ref');
$data->type = Input::get('type');
$data->komponen_bantu = Input::get('komponen_bantu');
$data->nama_komponen = Input::get('nama_komponen');
$data->spek = Input::get('spek');
$data->satuan = Input::get('satuan');
$data->diskon = Input::get('diskon');
$data->harga = Input::get('harga');
$data->pole = Input::get('pole');
$data->ka = Input::get('ka');
$data->ampere = Input::get('ampere');
$data->quantity = $quantity;
$data->harga_satuan_mu = $harga_satuan_mu;
$data->harga_satuan_mb = Input::get('harga_satuan_mb');
$data->harga_satuan_lb_oh = Input::get('harga_satuan_lb_oh');
$data->id_estimasi = Input::get('id_estimasi');
$data->nama_estimasi = Input::get('nama_estimasi');
$data->total = $total;
$data->trigger_bom = Input::get('trigger_bom');
$data->save();
}
}
My View
<td><input type="text" name="quantity[]" class="form-control"></td>
<td><input type="text" name="kode_proyek" hidden value="{{$panel->kode_projek}}">
etc....
My View
my view
You can save it first to array then use bulk insert.
Inserting data 1 by 1 will slow your application.
$insertArray = array();
foreach($request->quantity as $quantity){
if($quantity == NULL){
$insertArray[] = array(
'quantity' => $quantity,
'harga_satuan_mu ' => Input::get('harga_satuan_mu'),
.... // add other fields
)
}
}
KomponenOlahan::insert($insertArray);

How do I show multiple fields in a ViewBag drop down list for asp-items in asp.net-mvc-core

Model
public partial class Official
{
public short OfficialNo { get; set; }
public string Surname { get; set; }
public string Firstname { get; set; }
}
I have 2 partial answers but neither gives me what I want:
In my controller on Edit:
ViewBag.OfficialNo = new SelectList(_context.Official, "OfficialNo", "OfficialNo", "", "Surname");
This gives me
Smith
111
Brown
102
Gray
103
The top line is bold and comes from "Surname" which is supposed to be the Data Group Field. The bottom line is what is stored in the database and displays a value if there is one (which is correct).
Alternatively
ViewBag.OfficialNo = new SelectList(_context.Official.Select(e => e.OfficialNo + " - " + e.Firstname + " " + e.Surname));
This gives me
101 – John Smith
102 – Bob Brown
103 – Garry Gray
which is the info I want but does not allow a selection to be stored (naturally).
I want
John Smith
111
Bob Brown
102
Garry Gray
103
so that the first line is info and the second line (if selected) will be what is stored on update.
How do I combine the 2 ViewBag statements above ?
For further info my Edit View statement is:
<div class="form-group">
<label asp-for="RefereeNo" class="control-label"></label>
<select asp-for="RefereeNo" class="form-control" asp-items="ViewBag.OfficialNo"><option> </option></select>
</div>
One of the solution is to add Group property of SelectListItem :
Controller :
var officials = _context.Official.GroupBy(x => x.Firstname+" "+x.Surname);
List<SelectListItem> selectListItems = new List<SelectListItem>();
foreach (var official in officials)
{
var optionGroup = new SelectListGroup() { Name = official.Key };
foreach (var item in official)
{
selectListItems.Add(new SelectListItem() { Value = item.OfficialNo.ToString(), Text = item.OfficialNo.ToString(), Group = optionGroup });
}
}
ViewBag.OfficialNo = selectListItems;
Then in view show/bind the dropdownlist :
<div class="form-group">
<label asp-for="#item.RefereeNo" class="control-label"></label>
<select asp-for="#item.RefereeNo" class="form-control" asp-items="ViewBag.OfficialNo"><option> </option></select>
</div>
As per Nan Yu's answer above:
Controller :
var officials = _context.Official.GroupBy(x => x.Firstname+" "+x.Surname);
List<SelectListItem> selectListItems = new List<SelectListItem>();
foreach (var official in officials)
{
var optionGroup = new SelectListGroup() { Name = official.Key };
foreach (var item in official)
{
selectListItems.Add(new SelectListItem() { Value = item.OfficialNo.ToString(), Text = item.OfficialNo.ToString(), Group = optionGroup });
}
}
ViewBag.OfficialNo = selectListItems;

cf9 query grid values SQL

I have a grid bound to a cfc, populating an id column and a checkbox (boolean) column. onLoad, i want to get a list of ID values, but only those having checkbox = 1
Here is the working code, with great help from Jan S
<script language="JavaScript">
function init(){
var grid = ColdFusion.Grid.getGridObject('testgrid');
var gs = grid.getStore();
var records = gs.getRange();
var filteredRecords = [];
for (i = 0; i < records.length; i++) {
// note: for CF, you MUST capitalize grid header names
if (records[i].get('SELECT') === 1) {
var thisID = records[i].get('ID');
filteredRecords.push(thisID);
}
}
document.getElementById('idList').value=filteredRecords;
}
ColdFusion.Event.registerOnLoad(init,null,false,true);
</script>
<cfset items=QueryNew("id,Description,Select")>
<cfset Temp=QueryAddRow(items,1)>
<cfset Temp=QuerySetCell(items,"id","11")>
<cfset Temp=QuerySetCell(items,"Description","Some item")>
<cfset Temp=QuerySetCell(items,"Select",1)>
<cfset Temp=QueryAddRow(items)>
<cfset Temp=QuerySetCell(items,"id","22")>
<cfset Temp=QuerySetCell(items,"Description","Some other item")>
<cfset Temp=QuerySetCell(items,"Select",1)>
<cfset Temp=QueryAddRow(items)>
<cfset Temp=QuerySetCell(items,"id","33")>
<cfset Temp=QuerySetCell(items,"Description","A third item")>
<cfset Temp=QuerySetCell(items,"Select",0)>
<cfform>
<cfgrid name="testgrid" format="html" query="items">
<cfgridcolumn name="id" header="ID" select="no">
<cfgridcolumn name="Description" header="Description" select="no">
<cfgridcolumn name = "Select" header="Select" select="yes" type="boolean">
</cfgrid>
<br>
<!--- populate this with list of ID's having the checkbox checked, here: 11,22 --->
<input type="text" name="idList" id="idList"> <input type="button" name="getList" value="Get List" onClick="init()">
</cfform>
Basically I need to translate into AJAX this SQL statement:
select stringColumn where booleanColumn = 1 from myGrid
I'm using Cold Fusion 9 which is based on js ext 3.1 i believe
to get an array with all a field from all records where another field is 1:
isData.on('load', function(store, records){
// create a new array with all records where the 'booleanColumn' is = 1
var filteredRecords = [];
for (i = 0; i < records.length; i++) {
if (records[i].get('booleanColumn') === 1) {
filteredRecords.push(records[i]);
}
}
console.log(filteredRecords);
// create a new array of 'stringColumn' values
var filteredValues = [];
for (i = 0; i < filteredRecords.length; i++) {
filteredValues[i] = filteredRecords[i].get('stringColumn');
}
console.log(filteredValues);
// proceed with filtered values...
});

How to display array in MVC3?

Hello friends I get datas from Web services so I can't use Html.DisplayFor(model=>model.item)and how can I display datas in array. Here is my code:
#using icerik.TahakkukServices
#{
ViewBag.Title = "Deneme";
Layout = "~/Views/Shared/_Layout5.cshtml";
}
#{
TahakkukServicesClient client = new TahakkukServicesClient();
client.ClientCredentials.UserName.UserName = "service_test";
client.ClientCredentials.UserName.Password = "";
client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
MakbuzList[] liste = client.GetMakbuzListe(2);
}
#foreach (var item in liste)
{
Html.DisplayFor(item.Adi)
}
#model icerik.TahakkukServices. add your table name
#{
ViewBag.Title = "Deneme";
Layout = "~/Views/Shared/_Layout5.cshtml";
}
#{
TahakkukServicesClient client = new TahakkukServicesClient();
client.ClientCredentials.UserName.UserName = "service_test";
client.ClientCredentials.UserName.Password = "";
client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
MakbuzList[] liste = client.GetMakbuzListe(2);
}
#foreach (var item in liste)
{
Html.DisplayFor(model=>model.) add your column name
}
Allright I understand how to do it just giving model name with your Service as you can see at below
#model icerik.TahakkukServices.Borc
so you can use Html.DisplayFor

Razor and Html.DropDownList: Setting selected values for a dropdown for each row in a list?

This seems like it should be simple, but I can't figure out how to make it work.
My data model has a "Server" table, and a "ServerType" table. PKs for both tables are ints, and Server has a field ServerTypeId which is a fk to ServerType.Id.
I have a Razor List.cshtml that is typed to IEnumerable:
#model IEnumerable<Server>
<table border="1">
<tr>
<th>
Server Type
</th>
<th>
Name
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DropDownListFor(modelItem => item.ServerTypeId, (IEnumerable<SelectListItem>)ViewData["ServerType"])
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
</tr>
}
</table>
My controller has:
public ActionResult List()
{
var s = GetServers();
ViewData["ServerType"] = GetServerTypes();
return View("List", s);
}
private List<SelectListItem> GetServerTypes()
{
string id;
SelectListItem si;
List<SelectListItem> sl = new List<SelectListItem>();
IQueryable<ServerType> items = (from t in _entities.ServerTypes select t);
foreach (var item in items)
{
id = item.Id.ToString();
si = new SelectListItem { Value = id, Text = item.Description };
sl.Add(si);
}
return sl;
}
This displays the data, but the value in the dropdown is not selected. I've tried both Html.DropDownList and Html.DropDownListFor, with different permutation of names for the ViewData property, with and without the Id at the end.
Do I need to create a viewmodel that has copies of the ServerType in order to set the Selected property? Or is it a problem because my ids are ints, and the SelectItemList Value property is a string?
For anyone else still looking for he answer. I had to do 3 things to get this to work
User #for instead of #foreach. This is so it has an index to work
with when naming things.
Don't have the ViewBag variable name the
same as the property. It tries to help you out and binds things too
early if they have the same name.
Pass the current value in the constructor for SelectList. My
code ended up as:
#for (int i = 0; i < Model.Count; i++)
{
<tr>
<td>
#Html.DropDownListFor(modelItem => Model[i].operatorToken,
new SelectList(ViewBag.operatorTokensList, "Value", "Text", Model[i].operatorToken),
"Select", htmlAttributes: new { #class = "form-control" })
...ect...
}
Notice the 4th parameter to SelectList() sets the selected value.
My operatorTokensList was valued with:
new[] { new { Value = ">", Text = ">" },
new { Value = ">=", Text = ">=" },
new { Value = "=", Text = "=" },
new { Value = "<=", Text = "<=" },
new { Value = "<", Text = "<" } };
(The user was selecting "greater than", "greater than or equal", etc.)
At no point in your population of the List in GetServerTypes() do you specify that any of the items are selected. This is something you need to do manually, as MVC3 isn't smart enough to infer it for you in the DropDownListFor method. This is further complicated by the fact that you are not using a single model.
A better way to do this might be:
(Keep in mind in the below code, I'm assuming that the Server class has a primary id called "Id")
For the controller code:
public ActionResult List()
{
IEnumerable<Server> s = GetServers();
ViewData["ServerTypes"] = GetServerTypes(s);
return View("List", s);
}
private Dictionary<int, SelectList> GetServerTypes(IEnumerable<Server> s)
{
Dictionary<int, SelectList> sl = new Dictionary<int, SelectList>();
IEnumerable<ServerType> items = (from t in _entities.ServerTypes select t);
foreach (Server srv in s) {
sl.Add(srv.Id, new SelectList(items, "Id", "Description", srv.ServerTypeId));
}
return sl;
}
For the view code:
(Also note below how the I've corrected the arguments used in the lambda functions)
#model IEnumerable<Server>
<table border="1">
<tr>
<th>
Server Type
</th>
<th>
Name
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DropDownListFor(modelItem => modelItem.ServerTypeId, (IEnumerable<SelectListItem>)(ViewData["ServerTypes"][item.Id]))
</td>
<td>
#Html.DisplayFor(modelItem => modelItem.Name)
</td>
</tr>
}
</table>
#TreyE correctly points out that you never specify that any particular select list item should be selected when displayed in the view.
There are several ways you can do this. First is to use the SelectList object and use its constructor that allows you to pass in the object that should be selected, it's the overload SelectList(IEnumerable, String, String, Object) MSDN SelectList.
SelectList is only supported on .NET 3.5+ though FYI.
Second, in GetServerTypes() you could write:
private List<SelectListItem> GetServerTypes()
{
List<SelectListItem> sl = new List<SelectListItem>();
IQueryable<ServerType> items = (from t in _entities.ServerTypes select t);
foreach (var item in items)
sl.add(new SelectListItem { Value = item.id, Text = item.Description, Selected = item.isSelected } );
return sl;
}
Also remember that only one item should be selected, so make sure that if you do try to use some boolean property it is not possible that more than one item could have its isSelected property set to true.
Alternatively, if you need to use some type of if statement to decide if Selected = true (i.e. your item has no isSelected boolean) then you can add that in the foreach loop.
foreach(var item in items)
{
if //condition
sl.Add(new SelectListItem { Value = item.id, Text = item.Description, Selected = true });
else
sl.Add(new SelectListItem { Value = item.id, Text = item.Description, Selected = false });
}

Resources