Skip to main content

Posts

Showing posts with the label C#

C# print file on LAN Printer

If you want to print any file on network printer then you can try with this code block // Change Default Printer System.Management.ManagementObjectSearcher search = default(System.Management.ManagementObjectSearcher); System.Management.ManagementObjectCollection results = default(System.Management.ManagementObjectCollection); System.Management.ManagementObject printer = default(System.Management.ManagementObject); search = new System.Management.ManagementObjectSearcher("select * from win32_printer"); results = search.Get(); //Get Default Printer System.Management.ManagementObject defaultPrinter = null; foreach (System.Management.ManagementObject foundPrinter in results) { System.Management.PropertyDataCollection propertyDataCollection = foundPrinter.Properties; S...

C# dynamic linq query

In to Linq query, we can use dynamic query: public List<Insplan> GetInsplanLookupList(string planName = null, string binNo = null, string procCtrl = null) { var query = (from i in _db.insplan orderby i.cinsplanname ascending select i).AsQueryable(); if (!string.IsNullOrWhiteSpace(planName)) query = query.Where(x => x.cinsplanname.StartsWith(planName.Trim())); if (!string.IsNullOrWhiteSpace(binNo)) query = query.Where(x => x.cbinno.StartsWith(binNo.Trim())); if (!string.IsNullOrWhiteSpace(procCtrl)) query = query.Where(x => x.cprocctrlno.StartsWith(procCtrl.Trim())); return (from q in query join ic in _db.inscomp on q.inscompid_FK equals ic.inscompid_PK into ic_joined from ic in ic_joined.DefaultIfEmpty() join cl in _db.clinic on q.clinicid_FK equals cl.clinicid_PK into cl_joined from cl in cl_joined....

C# create folder on runtime

You can try this code: this will read data from app.config for folder path. if the the path is not found then it it will create folder. string curDirectory = ConfigurationManager.AppSettings["AppOutPath"]; if (Directory.Exists(curDirectory + @"\Uploads")) { // Do something } else { Directory.CreateDirectory(curDirectory + @"\Uploads"); // Do something }

The function import 'DBEntities' cannot be executed because it is not mapped to a store function.

1. Open EDMX in to Design Mode 2. Click Model Browser then type the Store procedure name on search and press Enter.    It will highlight the store procedure. 3. If you already try to add this store procedure, then in some case it create Function when you have a return value on your SP.  other wise it will not create the function. remove all from function. 3. If your SP has no return type then you may can use a simple return type value like :   SELECT 1 as DefaultValue   if you already have return type then you don't need that. 4. Select the Store procedure from model browser-> right click -> Add function Import.   5. Check Complex and Click Get column Information 6. Click on Create New Complex type. 7. It will create Function and complex type for that Store Procedure. Click OK and Build your application.

C# read word document and get specific data from document

// Reding all word document from a specific folder foreach (string file in Directory.EnumerateFiles(@"D:\", "*.doc")) { //string contents = File.ReadAllText(file); Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); object miss = System.Reflection.Missing.Value; object path = file; // @"D:\35339.doc"; object readOnly = true; Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss); string totaltext = ""; for (int i = 0; i < 2; i++) // i set value 2 you can change { totaltext += " \r\n " + docs.Paragraphs[i + 1].Range.Text.ToString(); } // Getting data after specif...

C# - Process.Start a ClickOnce application

string publisher_name = "ABCPublisher"; string product_name = "FaxManFMFFileCreator"; var shortcutName = string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.Programs), "\\",publisher_name, "\\", product_name, ".appref-ms"); Process.Start(shortcutName);

C# kill specific process

//Threading for speacific printer otherwise it will open default Faxman printer Task.Factory.StartNew(() => { Thread.Sleep(5000); // Killing Process of default printer Process[] processes = Process.GetProcessesByName("FaxManFMFFileCreator"); foreach (var process in processes) { process.Kill(); } });

The calling thread must be STA, because many UI components require this.

Using Thread: // Create a thread Thread newWindowThread = new Thread(new ThreadStart(() => { // You can use your code // Create and show the Window FaxImageLoad obj = new FaxImageLoad(destination); obj.Show(); // Start the Dispatcher Processing System.Windows.Threading.Dispatcher.Run(); })); // Set the apartment state newWindowThread.SetApartmentState(ApartmentState.STA); // Make the thread a background thread newWindowThread.IsBackground = true; // Start the thread newWindowThread.Start(); Using Task and Thread: // Creating Task Pool, Each task will work asyn and as an indivisual thread component Task[] tasks = new Task[3]; // Control drug data disc UI load optimize tasks[0] = Task.Run(() => { //This will handle the ui thread :The calling thread must be STA, because many U...

ASP.NET MVC Generate Crystal report to pdf

See the code sample: public ActionResult GenerateReport(CustomerPackageReportViewModel CustomerPackageReportViewModel) { List<CustomerPackageDetailsReportViewModel> _customerList = (List<CustomerPackageDetailsReportViewModel>)Session["CustomerPackageInfo"]; DataTable tableObj = new DataTable(); if (_customerList!=null) { tableObj = Converter.ToDataTable(_customerList); } else { tableObj = Converter.ToDataTable(ArchitectureList); } ReportDocument reportDoc = new ReportDocument(); string rptPath = ""; rptPath = Server.MapPath("~/Reports/Billing/Crystal/rptCustomerPackage.rpt"); reportDoc.Load(rptPath); reportDoc.SetDataSource(tableObj); Stream reportStream = this.ConvertReportToPDF(reportDoc); return new FileStreamResult(reportStream, "application/pdf"); ...

asp.net mvc generate rdlc/Excel to pdf report

You can follow the code: public ActionResult GenerateReport(CustomerPackageReportViewModel CustomerPackageReportViewModel) { List<CustomerPackageDetailsReportViewModel> _customerList = (List<CustomerPackageDetailsReportViewModel>)Session["CustomerPackageInfo"]; DataTable tableObj = new DataTable(); if (_customerList!=null) { tableObj = Converter.ToDataTable(_customerList); } else { tableObj = Converter.ToDataTable(ArchitectureList); } ReportDataSource rds = new ReportDataSource("dsCustomerPackageInfo", tableObj); ReportViewer localReport = new ReportViewer(); localReport.ProcessingMode = ProcessingMode.Local; localReport.LocalReport.ReportPath = Server.MapPath("~/Reports/Billing/rdlc/rptCustomerPackageInfo.rdlc"); localReport.LocalReport.DataSources.Add(rds); // Add datasource here stri...

asp.net mvc add & remove item from html table

To add & remove item in HTML table like this image you can like the this: Model: public class StudentDetailsModels { public System.Guid? StudentId { get; set; } public System.String StudentName { get; set; } public System.String StudentFatherName { get; set; } public System.String StudentAddress { get; set; } } public class StudentModels { [Key] public System.Guid? StudentId { get; set; } public System.String StudentName { get; set; } public System.String StudentFatherName { get; set; } public System.String StudentAddress { get; set; } private List<StudentDetailsModels> _studentDetailsList = new List<StudentDetailsModels>(); public List<StudentDetailsModels> StudentDetailsList { get { return _studentDetailsList; } set { _studentDetailsList = value; } } } View: @{ ViewBag.Title = "StudentInfo"; ...

how to pass the multiple values in session ASP.NET

Set Value in session // Set value in one UI in session string empIdList = GetSelectedIDofGrid(); var paramObjects = new Dictionary<string, object> { {"paramEmployeeXmlstr", empIdList}, {"paramFactoryId", ddlFactoryName.SelectedValue} }; Session["paramPayroll_RPT014_Staff"] = paramObjects; Get value from session // Get Value in another UI From session private string _paramEmployeeXmlstr = String.Empty; private string _paramFactoryId = String.Empty; var paramObjects = Session["paramPayroll_RPT014_Staff"] as Dictionary<string, object>; if (paramObjects != null) { _paramEmployeeXmlstr = (string)paramObjects["paramEmployeeXmlstr"]; _paramFactoryId = (string)paramObjects["paramFactoryId"]; }

C# allow nullable value get from EF

You can get value from Entity framework which can allow null able and you can also perform filtering in your query. private static IQueryable<EmployeeInformation> ReportFilteringEmployeeInformations(Guid factoryId, Guid departmentId, Guid designationId, string empId, string empName, Guid sectionId, Guid categoryEmpId, int? CategoryTypeID, Guid categorySalaryID, Guid blockID,string cardNo, PayrollEntities context) { var query = from emp in context.vw_PIS_TblEmployeeGenInfo join des in context.vw_PIS_TblEmployeeDesignation on emp.GDesignationInfoID equals des.GDesignationInfoID into des_J join dept in context.vw_PIS_tblDepartment on emp.GDepartmentID equals dept.GDepartmentID into dept_J join sec in context.vw_PIS_TblSection on emp.SectionID equals sec.SectionID into sec_J from des1 in des_J.DefaultIfEmpty() from dept1 in dept_J.DefaultIfEmpty() from sec1 in sec_J.D...