Textbox text as Object's name - c#

I have a TextBox named "tb1" (Not real name).
I want to, when I click a button; create an "Product" object with the value of the "tb1" text.
Something like...
Product tb1.text = new Product();
How do I do it ?

You need to override the constructor on the Product object so that it can accept a string parameter.
Product object:
public Product(string productName)
{
//set product name variable using productName parameter
_productName = productName;
}
And then you can do something like this:
//set product name using new constructor
Product product = new Product(tb1.Text);

Pass "tb1.text" to Product's constructor while creating with new keyword.

Your example is trying to create a Product where the variable name is the content of tb1.Text. Far as I know, thats impossible. It'd be really confusing for anybody reading the code later anyway, so even if it is possible please don't do it. :)
If you're just trying to create a Product where the Product name is set to the value of tb1.Text, you can do this:
Product someProduct = new Product();
someProduct.Name = tb1.Text;
Or pass into the constructor like Arun mentioned.

Related

How to update just selected field in the passed ViewModel

I have a viewmodel to update data (from API, not view) with params like below:
public string Name { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
i just want to update "name", so my param like below:
{
"name": "my name"
}
its name changed become "my name" but its email and phone become null. how to avoid params changed to be null if they don't exist in input form?
Thanks...
You'll have to adjust whatever does the update so that it understands "null means do not set a value, rather than set the value to null"
For example if you're running a db update query you could:
UPDATE person
SET
name = COALESCE(#name, name),
email = COALESCE(#email, email),
phone = COALESCE(#phone, phone)
WHERE
id = #id
Now if any value is supplied as null the update will set the column to the same value it is currently (ie no-op)
If you're adjusting a c# object you can take a similar approach:
var p = db.FindPerson(viewmodel.PersonId);
p.Name = viewmodel.Name ?? p.Name;
...
As I see, you have a problem or misunderstanding in your software design.
If this API endpoint is meant to just update the name field, then you should not have the other fields in your ViewModel. However, if you update the other fields in some cases, then you should pass their values as well.
So maybe you need to call a Get endpoint first to get the all data you need in your client "web page for example" and then allow this client to resend the full JSON, not just the name.
Or you may just need an endpoint that just takes the name.
Another solution, in case you're using EF, is to ignore the null fields when updating the EF entity.
e.g.
...
var entity = dbContext.Employees.FirstOrDefault(e => e.Id == 3);
entity.Name = request.Name;
await dbContext.SaveChangesAsync();
...

How to let a user add new objects in a list?

In my Console App the user can currently add a new object by pressing the "+" key, but how can I let him later on change a specific object in the list? For example.
The list is of boats. I want each boat to continously get a specific name, i.e boat1, boat2, boat3, etc. And if a user adds another boat it will get then name boat4, and so on, so that he later in the application can change the speed of a specific boat by calling its name.
Boats boat1 = new Boats();
boat1.Speed = 15;
Boats boat2 = new Boats();
boat2.Speed = 26;
List<Boats> boatList = new List<Boats>();
boatList.Add(boat1);
boatList.Add(boat2);
And in the Boats.cs I have this method:
public static void NewBoat()
{
var newBoat = new Boats();
Console.WriteLine("\nIf you want to create a new boat, press +");
if (Console.ReadKey().KeyChar == '+')
{
var newBoats = new Boats();
newBoat.AddBoatList(newBoats);
}
else
{
Console.WriteLine("\nInvalid Input.");
}
boat1 and boat2 are just variable names, not the name of the object. If you add them to the list the list doesn't know anything about the names. If you want to have the name in the list you should have a String with the name as part of the Boats class to which you assign the name. Later you can search for the name in the list.
To generate the variables it's better to do it in the way of your second part in Boats.cs: not for each boat an extra variable, just one variable which process the new before you assign the data to it and add it to the list.
You need to save the names of the boats in the list and then you can create a function to retrieve the boat name and then you can change it

How to Update a Field in NetSuite suite talk without its internal id giving its internal id value

I have tried to create a opportunity with suite talk API. while updating the entity field value it returns error because it needs internal id value of the field but it is not feasible to address the internal id.
ReflectionExtensions.SetPropertyValue(NS_OPPURTUNITY, map.Dst_Fld_Name, new RecordRef()
{
internalId = "2551",
type = RecordType.customer,
typeSpecified = true
});
i want to get rid of that static id to reference the entity.
As far as I know, you need the internal ID to reference any object through web services. You can however find the internal ID by first searching for the item you need to reference.
You can use a CustomerSearch to find the internal ID of your customer:
CustomerSearch custSearch = new CustomerSearch();
SearchStringField name = new SearchStringField();
name.SearchValue = "firstName";
name.operatorSpecified = true;
name.#operator = SearchStringFieldOperator.#is;
CustomerSearchBasic custBasic = new CustomerSearchBasic();
custBasic.firstName= customerEntityID;
custSearch.basic = custBasic;
// Search for the customer entity
SearchResult res = _service.search(custSearch);
//Get the internal ID of the customer
string internalID = ((Customer) (res.recordList[0])).internalId;
You can search for the customer using other fields besides 'firstName' as well. Check which other fields are available on the CustomerSearchBasic object here: CustomerSearchBasic
You can update the entity field on basis of external ID also but for this you need to remember certain things
1. You need to set externalID during creation of any record.
2. External ID is unique around the system.
3. Some records don't support external ID such as Custom List.
InventoryItem inventory = new InventoryItem();
inventory.externalId = "abc";
inventory.displayname = "Hello";
setPreferences();
WriteResponse writeRes = _service.update(inventory );

address an object by retrieving the ID name of the object from a variable

Good evening,
I am trying to get the following done. I have seen a similar post but it was related with Unity.
Anyway, I am on web forms in asp.net and I have a radiobuttonList with ID="id001"
so on my code behind, I would normally be able to get the selected value by just doing:
string value = id001.SelectedValue
However, in this situation, I don't know the exact ID name, so I have a function that retrieves it. So now I have the variable with the name of the ID. So I want to be able to now, convert the value of that variable in something like this:
string foundid = "id001"
string foundidvalue = id001.SelectedValue
I hope this makes sense.
Thanks in advance for the help.
I am assuming this one is related to your previous question. So, when you found the control, instead of using function to get the fullname, you can do like this:
foreach (Control c in Page.Form.Controls.OfType<RadioButtonList>())
{
if (c.ID.Contains("id"))
{
// string FullID = c.ID.ToString();
var radioButtonList = c as RadioButtonList;
var selectedValue = radioButtonList.SelectedValue;
}
}
You want to use FindControl.
string foundid = "id001";
var foundCtrl = (RadiobuttonList)FindControl(foundid);
var result = foundCtrl.SelectedValue;

Copy a text field data into a look field type

I am new in coding world :) it would be helpful if somebody can help me with my below query.
We have created two custom entity called Sector and Sub sector which are having an 1:N relationship with Account. Since they are relation fields hence they are lookup type and populated on the Account form.
On another part, we have InsideView( 3rd party tool) integrated with our Contact and account form. We have mapped certain fields from Inside view with CRM fields to update the data from the inside view when it is being synced however Inside view does not support Lookup type field hence we cannot map lookup field type data.
We discovered this barrier recently when we tried to map our custom entity (sector and Subsector) with inside view. Since we cannot map lookup field type we thought to have two text field instead and map it with Inside view. Once data is synced these two text fields will get filled out with the sector and subsector name.
Now, we want to copy information from text fields to the lookup field (custom fields Sector and Sub sector)
thanks in advance for your help :)
Bhavesh
I am not understanding your requirement but you can fill a lookup field with some text.
Use following code:
function setLookupField() {
var context = Xrm.Page.context;
var UserID = context.getUserId();// your id
var UserName=context.getUserName();//your text
var lookupData = new Array();
var lookupItem = new Object()`enter code here`;
//Set the GUID
lookupItem.id = UserID;
//Set the name
lookupItem.name = UserName;
lookupItem.entityType = "systemuser";// entity name of lookup
lookupData[0] = lookupItem;
//If existing value is empty, then set new value
var existingValue = Xrm.Page.getAttribute("new_usercreatedby").getValue();
if (existingValue === null) {
Xrm.Page.getAttribute("new_usercreatedby").setValue([{
id: UserID,
name: UserName,
entityType: "systemuser"
}]);
} else {
return;
}
}

Categories

Resources