Getting error when passing value into object - c#

Here is my code within my ASP.net project. I am trying to store some values into my object with my web form, but it pops up an error message saying: Cannot implicitly convert type 'short' to 'string'.
textbox.Text = Convert.ToInt16(object.number);
lstbox.SelectedValue = Convert.ToInt16(object.ID);
Within my object class, I have declared my variables to int. Please let me know what is wrong.

The Text and SelectedValue properties are strings. Why would you convert the values to short in the first place?
textbox.Text = object.number.ToString();
lstbox.SelectedValue = object.ID.ToString();

You can't assign non-string values to a property which accepts string/text values. In your case, you are trying to assign a short value to text property of textbox. Please cast the value to string using Convert.ToString or ToString().
So your code should be
textbox.Text = Convert.ToString(object.number);
or
textbox.Text = object.number.ToString();
Scenario is same while assigning the selected value property of listbox.
lstbox.SelectedValue = Convert.ToString(object.ID);

Since you want to pass the value to the object, you should have the object's variables = the input value.
object.number = Convert.ToInt16(textbox.Text);
object.ID = Convert.ToInt16(lstbox.SelectedValue);

Related

How do I convert a System.Windows.Forms.Control into an integer in C#?

I use Controls.Find to get the value of a textbox which I don't know the name of until runtime. However the result returned by Controls.Find cannot seem to be converted into a string OR an integer (ERROR MSG:
CS1503 Argument 1: cannot convert from 'System.Windows.Forms.Control' to 'string'). I thought that if I could convert it to string first then it wouldnt be a problem converting to integer but it wont even let me convert it to string. Most mysteriously though, it prints this value to screen when I use Console.WriteLine(tbValue)! Can anyone help?
//This function takes the name of a textbox from an array and returns the value of it
int GetDynamicTextboxValue(String tbName)
{
int value = 0;
var matches = this.Controls.Find(tbName, true); //although matches is an array we are only expecting one result
foreach (var tbValue in matches)
{
Console.WriteLine("Dynamic textbox value = " + tbValue);
value = Int32.Parse(tbValue);//This is where the conversion error occurs
}
return value;
}
You actually have Controls collection at the moment which is the base class and of all the controls like TextBox, Label.
You will need to connvert the Control object to TextBox and then get the value from Text property of TextBox class something like:
foreach (var tbValue in matches)
{
Console.WriteLine("Dynamic textbox value = " + tbValue);
// cast the control as TextBox object
var textBox = tbValue as TextBox;
// if casting successful textBox will have reference to that textBox
if(textBox !=null)
value = Int32.Parse(textBox.Text);// now get the value of textbox and convert to integer
}

how is the error if the error is cannot implicitly convert type 'string ' to System.Window.Forms in c#

I dont get it please help me in line of lblGrandTotal = ""; it said
Cannot Implicitly convert type 'string ' to System.Window.Forms.Label
It should be as below :
lblGrandTotal.Text = "";
You should set the .Text property of the control lblGrandTotal:
lblGrandTotal.Text = "";
use lblGrandTotal.Text = "yourText";
You are trying to assign text to a Label object type.
You need to set the Text property. Furthermore, it's better code if you do lblGrandTotal.Text = string.Empty rather than "". Nit picking, but better code.

Setting string values to Viso shapedata

I have a visio shape with shape data column Prop.Name of type string
when i try to set its value using
Visio.Cell propCell = _shapeList[i].get_Cells("Prop.Owner");
propCell.FormulaForceU = "asd";
I get an error: #NAME?
This does not happen if i pass a numeric string .
How can i pass characters other than number ?
propCell.FormulaForceU = "\"asd\"";
I am trying to update the shape property value with the normal string as like 'Set Value' it gives error #NAME?.
After giving
c.Formula ="\"Set Value\"";
it begins to work.

WPF DataBinding: Nullable Int still gets a validation error?

I have a textbox databound to a nullable int through code. If I erase the data from the textbox it gives me a validation error (red border around it).
Here is my binding code:
ZipBinding = new Binding("Zip");
ZipBinding.Source = Address;
zipTextBox.SetBinding(TextBox.TextProperty, ZipBinding);
public Int32? Zip { get { ... } set { ... } }
It's clearly marked as a Nullable so why does WPF wanna give me a validation issue when I clear the textbox?
Validation is failing because it can't convert the empty string to a nullable integer. Set TargetNullValue to string.empty on the Binding and it will convert the empty string to null, which will be valid.
An empty TextBox != null.
You may have to tweak the ValidationRule to accommodate empty strings as entries. Or, you could create a converter to take empty strings and convert them to null.

C# nullable string error

private string? typeOfContract
{
get { return (string?)ViewState["typeOfContract"]; }
set { ViewState["typeOfContract"] = value; }
}
Later in the code I use it like this:
typeOfContract = Request.QueryString["type"];
I am getting the following error at the declaration of typeOfContract line stating:
The type 'string' must be a non-nullable value type in order to use
it as parameter 'T' in the generic type or method
'System.Nullable<T>'
Any ideas? Basically, I want to make sure that "type" exists in the QueryString before performing an action.
System.String is a reference type and already "nullable".
Nullable<T> and the ? suffix are for value types such as Int32, Double, DateTime, etc.
You are making it complicated. string is already nullable. You don't need to make it more nullable. Take out the ? on the property type.
string cannot be the parameter to Nullable because string is not a value type. String is a reference type.
string s = null;
is a very valid statement and there is not need to make it nullable.
private string typeOfContract
{
get { return ViewState["typeOfContract"] as string; }
set { ViewState["typeOfContract"] = value; }
}
should work because of the as keyword.
String is a reference type, so you don't need to (and cannot) use Nullable<T> here. Just declare typeOfContract as string and simply check for null after getting it from the query string. Or use String.IsNullOrEmpty if you want to handle empty string values the same as null.
For nullable, use ? with all of the C# primitives, except for string.
The following page gives a list of the C# primitives:
http://msdn.microsoft.com/en-us/library/aa711900(v=vs.71).aspx

Categories