How to pass a class into another class to a codebehind?
When I debug and check the myCategoryObj in the Default.aspx page, I can see the object is in the debug. What am I doing wrong?
I know I could create the object in the Default.aspx but I should not have to I should be able to call the Business Logic Layer and ask for an object back and then fill the object and pass it back to the Business Logic Layer to be saved (insert or update).
I hope this makes sense.
Default.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SC1.Models.OBJ;
using SC1.Models.BLL;
using SC1.Models.DAL;
namespace SC1
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(Object sender, EventArgs e)
{
// I know I could do this but I don't want to unless I have too.
//Category categoryObj = new Category();
CategoryBLL myCategoryBLL = new CategoryBLL();
Object myCategoryObj = myCategoryBLL.CategoryNew();
// How do I make the code below work or what am I doing wrong.
myCategoryObj.Name = "test";
string test = "";
}
}
}
CategoryBLL.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using SC1.Models.DAL;
using SC1.Models.OBJ;
namespace SC1.Models.BLL
{
public class CategoryBLL
{
// Create a page object
Category myCategoryObject = new Category();
// Create a Data Acces Layer Object
CategoryDAL myCategoryDAL = new CategoryDAL();
public CategoryBLL()
{
}
public DataSet Select()
{
return (myCategoryDAL.Select());
}
public Object CategoryNew()
{
return myCategoryObject;
}
}
}
CategoryDAL.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace SC1.Models.DAL
{
public class CategoryDAL
{
public CategoryDAL()
{
}
string connStr = ConfigurationManager.ConnectionStrings["staceys_cakesConnectionString"].ConnectionString;
// select all
public DataSet Select()
{
SqlConnection sqlConnection1 = new SqlConnection();
string SqlString = "select * from Categories";
SqlDataAdapter da = new SqlDataAdapter(SqlString, connStr);
DataSet ds = new DataSet();
da.Fill(ds, "Categories");
return (ds);
}
// save
// insert
// update
// delete
}
}
Category.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SC1.Models.OBJ
{
public class Category
{
public int CategoryID { get; set; }
public int ParentID { get; set; }
public string Name { get; set; }
public int DisplayOrder { get; set; }
public bool Active { get; set; }
public Category(){
}
}
}
change
Object myCategoryObj = myCategoryBLL.CategoryNew()
to
Category myCategoryObj = myCategoryBLL.CategoryNew()
and also
public Object CategoryNew()
{
return myCategoryObject;
}
to
public Category CategoryNew()
{
return myCategoryObject;
}
Related
I have a class in the Dashboard.App_code
The functions in this class can be called without problem from the web form (bugs.aspx) to populate object data sources
But when I try to call a function from bugs.aspx.cs, I get the follwoing error message
Compiler Error CS0103 The name 'Clmysql' does not exist in the current context
bugs.aspx.cs
using System;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
namespace Dashboard
{
public partial class _Bugs : System.Web.UI.Page
{
protected void Calendar2_SelectionChanged(object sender, EventArgs e)
{
txtdate2.Text = Calendar2.SelectedDate.ToString("dd-MM-yyyy");
if (txtdate1.Text != "" && txtdate2.Text != "")
{
GridView1.DataBind();
GridView2.DataBind();
txtjson.Text = **Clmysql.GettreatedBugsdetails**(Calendar1.SelectedDate, Calendar2.SelectedDate);
}
}
}
app_codes\Clmysql.cs
using System.Data;
using MySql.Data.MySqlClient;
using System;
using System.Configuration;
using Newtonsoft.Json;
namespace Dashboard.App_code
{
public static class Clmysql
{
public static string GettreatedBugsdetails(DateTime Mydate1, DateTime Mydate2)
{
var conn = new MySql.Data.MySqlClient.MySqlConnection(ConfigurationManager.ConnectionStrings["mantis"].ConnectionString);
conn.Open();
string StrQuery = "select *****mysql code here" max(date(FROM_UNIXTIME(date_modified))),realname";
MySqlCommand Command = new MySqlCommand(StrQuery, conn);
DataTable dt = new DataTable();
using (MySqlDataAdapter da = new MySqlDataAdapter(Command))
{
da.Fill(dt);
}
conn.Close();
conn.Dispose();
return JsonConvert.SerializeObject(dt);
}
}
}
ASP.NET C# Web Forms Visual Studio 2019
After searching, I found the solution
Right-click on the class file "ClMysql.cs" => properties=>Build action: compile instead of content
i am trying to make an angular application that takes an api made from asp.net core but while making the api , it didn't work and appear as planned and didn't know where was the problem...
I made an asp.net core web app.
This is the student.cs file made in the model folder
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebAPI101.Model
{
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public bool Pass { get; set; }
}
}
This is the studentmanager also in model folder
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebAPI101.Model
{
public class StudentManager
{
public List<Student> GetResults()
{
List<Student> oList = new List<Student>();
var r = new Random();
for(int i = 0; i < 10; i++)
{
var x = new Student();
x.ID = i;
x.Name = String.Format("Name{0}", i, ToString());
x.Pass = (r.Next() % 2 == 0);
oList.Add(x);
}
return oList;
}
}
}
This is the code that should return aaaaa
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebAPI101.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class StudentController : ControllerBase
{
[Route("mariam")]
public string test()
{
return "aaaaa";
}
}
}
I tried to launch the code and it didn't work as planned o the link:https://localhost:5001/api/Student/mariam
The problem is you are using [Route("mariam")] which overrides the controller route.
You need to use [HttpGet("mariam")] instead.
[HttpGet("mariam")]
public string test()
{
return "aaaaa";
}
Using HttpGet will add to the controller route.
This question already has answers here:
Append lines to a file using a StreamWriter
(11 answers)
Closed 1 year ago.
I need to create a method called publish, to get the name, description, endpoint, no of operands and operand type in JSON format and write it to a text file. I have implemented that part but after the first API call the text in the text file is getting over written which is not what I want. I have included the model class and the controller class below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Registry.Models
{
public class publishModel
{
public string Name { get; set; }
public string Description { get; set; }
public string endpoint { get; set; }
public int NoOfoperands { get; set; }
public string operandType { get; set; }
}
}
using Newtonsoft.Json;
using Registry.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Web.Http;
namespace Registry.Controllers
{
public class publishController : ApiController
{
[Route("api/publish")]
[HttpPost]
public string publish(publishModel pModel)
{
string servicePath = #"C:\Users\ASUS\source\repos\DC assignment 1\Services.txt";
MemoryStream ms = new MemoryStream();
var write = new Utf8JsonWriter(ms);
write.WriteStartObject();
write.WriteString("Name", pModel.Name);
write.WriteString("Description", pModel.Description);
write.WriteString("Endpoint", pModel.endpoint);
write.WriteString("No of operands", pModel.NoOfoperands.ToString());
write.WriteString("Operand type", pModel.operandType);
write.WriteEndObject();
write.Flush();
string json = Encoding.UTF8.GetString(ms.ToArray());
//string json = JsonConvert.SerializeObject(pModel);
try
{
using (StreamWriter writer = new StreamWriter(servicePath))
{
writer.Write(json);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return "Description saved";
}
}
}
https://learn.microsoft.com/en-us/dotnet/api/system.io.streamwriter.-ctor?view=net-5.0#System_IO_StreamWriter__ctor_System_String_System_Boolean_
Add an extra argument to your StreamWriter:
using (StreamWriter writer = new StreamWriter(servicePath, true))
That tells it you open the path for writing, but append to the file. When you don't pass in a Boolean (which is what you are doing) it defaults to overwriting the file.
I already create new custom field for screen Fixed Asset. The following code is my DAC extension:
using PX.Data;
using PX.Objects.CR;
using PX.Objects.CS;
using PX.Objects.EP;
using PX.Objects.FA;
using PX.Objects.GL;
using PX.Objects;
using System.Collections.Generic;
using System;
namespace SGLCustomizeProject
{
public class FALocationHistoryExtension : PXCacheExtension<PX.Objects.FA.FALocationHistory>
{
#region UsrKodeArea
[PXDBString(50)]
[PXUIField(DisplayName = "Kode Area")]
[PXSelector(typeof(Search<KodeAreaMaster.roomCD,
Where<KodeAreaMaster.status,
Equal<statusActive>,
And<KodeAreaMaster.buildingID,
Equal<Current<FALocationHistory.buildingID>>>>>),
typeof(KodeAreaMaster.roomCD),
typeof(KodeAreaMaster.roomDescription),
typeof(KodeAreaMaster.buildingID),
typeof(KodeAreaMaster.status))]
public virtual string UsrKodeArea { get; set; }
public abstract class usrKodeArea : IBqlField { }
#endregion
#region UsrDeskripsiArea
[PXDBString(75)]
[PXUIField(DisplayName = "Deskripsi Area")]
public virtual string UsrDeskripsiArea { get; set; }
public abstract class usrDeskripsiArea : IBqlField { }
#endregion
}
}
I need to fill the selected value into another additional field in the current screen, please see the following screenshot:
I need to fill value of Deskripsi Area from selector field (pop up) into Deskripsi Area field.
I have tried the following code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using PX.Common;
using PX.Data;
using PX.Objects.CS;
using PX.Objects.CR;
using PX.Objects.CM;
using PX.Objects.GL;
using PX.Objects.AP;
using PX.Objects.EP;
using PX.Objects.IN;
using PX.Objects.FA.Overrides.AssetProcess;
using PX.Objects;
using PX.Objects.FA;
namespace SGLCustomizeProject
{
public class AssetMaint_Extension : PXGraphExtension<AssetMaint>
{
public virtual void _(Events.FieldUpdated<FALocationHistory, FALocationHistoryExtension.usrKodeArea> e)
{
var row = e.Row;
var ext = row.GetExtension<FALocationHistoryExtension>();
e.Cache.SetValue<FALocationHistoryExtension.usrDeskripsiArea>(row, ext.UsrKodeArea);
}
}
}
This code above has been worked, but the result is when I choose Kode Area field, it also fill into Deskripsi Area field. My goal is to fill Deskripsi Area with the same field (Deskripsi Area) from selector field.
I tried to change code above with the following code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using PX.Common;
using PX.Data;
using PX.Objects.CS;
using PX.Objects.CR;
using PX.Objects.CM;
using PX.Objects.GL;
using PX.Objects.AP;
using PX.Objects.EP;
using PX.Objects.IN;
using PX.Objects.FA.Overrides.AssetProcess;
using PX.Objects;
using PX.Objects.FA;
namespace SGLCustomizeProject
{
public class AssetMaint_Extension : PXGraphExtension<AssetMaint>
{
public virtual void _(Events.FieldUpdated<FALocationHistory, FALocationHistoryExtension.usrKodeArea> e)
{
var row = e.Row;
var ext = row.GetExtension<FALocationHistoryExtension>();
e.Cache.SetValue<FALocationHistoryExtension.usrDeskripsiArea>(row, ext.UsrDeskripsiArea);
}
}
}
But it doesn't work. Any step that I forget ?
Change the ALocationHistoryExtension_UsrKodeArea_FieldUpdated to ALocationHistory_UsrKodeArea_FieldUpdated
protected virtual void FALocationHistory_UsrKodeArea_FieldUpdated(PXCache sender, PXFieldUpdatedEventArgs e)
{
if (e.Row is FALocationHistory)
{
sender.SetDefaultExt<FALocationHistoryExtension.usrDeskripsiArea>(e.Row);
}
}
To Override an Event Handler
use xxx_fieldUpdated event
In your aspx page check, that you have commitchanges set to true
[ as usually optional ] sometime is necessary to set autorefresh = true
Starting from 2017 R2 you can also use this approach:
public virtual void _(Events.FieldUpdated<FALocationHistory, FALocationHistoryExtension.usrKodeArea> e)
{
var row = e.Row;
var ext = row.GetExtension<FALocationHistoryExtension>();
e.Cache.SetValue<FALocationHistoryExtension.usrDeskripsiArea>(row, ext.UsrKodeArea);
var KodeAreaMaster =
PXSelect<KodeAreaMaster, Where<KodeAreaMaster.roomCD, Equal<Required<KodeAreaMaster.roomCD>>>>
.Select(Base, ext.UsrKodeArea).First().GetItem<KodeAreaMaster>();
e.Cache.SetValueExt<FALocationHistoryExtension.usrDeskripsiArea>();
}
I used the following code to provide my goal. Thanks for everyone for all the respons. :)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using PX.Common;
using PX.Data;
using PX.Objects.CS;
using PX.Objects.CR;
using PX.Objects.CM;
using PX.Objects.GL;
using PX.Objects.AP;
using PX.Objects.EP;
using PX.Objects.IN;
using PX.Objects.FA.Overrides.AssetProcess;
using PX.Objects;
using PX.Objects.FA;
namespace SGLCustomizeProject
{
public class AssetMaint_Extension : PXGraphExtension<AssetMaint>
{
public virtual void _(Events.FieldUpdated<FALocationHistory, FALocationHistoryExtension.usrKodeArea> e)
{
var row = e.Row;
var ext = row.GetExtension<FALocationHistoryExtension>();
if (ext.UsrKodeArea != null)
{
e.Cache.SetValue<FALocationHistoryExtension.usrDeskripsiArea>(row, ext.UsrKodeArea);
var kodeAreaMaster = PXSelect<KodeAreaMaster,
Where<KodeAreaMaster.roomCD,
Equal<Required<KodeAreaMaster.roomCD>>>>
.Select(Base, ext.UsrKodeArea).First().GetItem<KodeAreaMaster>();
e.Cache.SetValueExt<FALocationHistoryExtension.usrDeskripsiArea>(row, kodeAreaMaster.RoomDescription);
}
}
public virtual void _(Events.FieldUpdated<FALocationHistory.buildingID> e)
{
var row = e.Row as FALocationHistory;
var ext = row.GetExtension<FALocationHistoryExtension>();
if (row.BuildingID != null)
{
if (ext.UsrKodeArea != null)
{
var kodeAreaMaster = PXSelect<KodeAreaMaster,
Where<KodeAreaMaster.roomCD,
Equal<Required<KodeAreaMaster.roomCD>>>>
.Select(Base, ext.UsrKodeArea).First().GetItem<KodeAreaMaster>();
if (row.BuildingID == kodeAreaMaster.BuildingID)
{
return;
}
else
{
e.Cache.SetValueExt<FALocationHistoryExtension.usrKodeArea>(row, null);
e.Cache.SetValueExt<FALocationHistoryExtension.usrDeskripsiArea>(row, null);
}
}
}
else
{
e.Cache.SetValueExt<FALocationHistoryExtension.usrKodeArea>(row, null);
e.Cache.SetValueExt<FALocationHistoryExtension.usrDeskripsiArea>(row, null);
}
}
}
}
Even using an example from the Documentation, I still can't find a way to successfully serialize to a file.
Code:
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;
using System.Threading;
namespace TestProject
{
class Program
{
static void Main(string[] args)
{
Item i = new Item
{
Username = "user",
Email = "user#user.com",
Password = "password"
};
File.WriteAllText(#"C:\users\user1.json", JsonConvert.SerializeObject(i));
using (StreamWriter file = File.CreateText(#"C:\users\user1.json"))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, i);
}
}
}
}
Item.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestProject
{
public class Item
{
public string Email { get; set; }
public string Password { get; set; }
public string Username { get; set; }
}
}
When I run Program.cs, it shows no errors, but the JSON does not show in the file.
When I run your code I get a an exception writing to the users folder. Changing to my home folder works fine. I suspect this is your problem.
Addtionally, you're writing the file twice. The first time is showing an example of using the SerializeObject method to get a string back which is used with WriteAllText. The second block of code is using a StreamWriter. For your purposes, both are equivalent and you only need to use one or the other.
using (StreamWriter file = File.CreateText(#"C:\users\user1.json"))
{
var jsonResult = JsonConvert.SerializeObject(i);
file.WriteLine(jsonResult);
}