convert datagridview value to int - c#

I am getting value from cell within datagridview as below and I am convert that value to int first I used the 'Convert.ToInt32' and I get error that says
Input string was not in a correct format.
I searched and found that I have to use int.tryParse
ImgId = Int32.TryParse(DGV_PatientImages.Rows[RowIndex].Cells[4].Value, out ImgId);
but I got an error saying
The best overloaded method match for 'int.TryParse(string, out int)'
has some invalid arguments
int ImgId = 0;
so I tried to user (int) Specified cast is not valid
ImgId = (int)DGV_PatientImages.Rows[RowIndex].Cells[4].Value;
and I got
Specified cast is not valid
DataGridViewRow dgvrow = DGV_PatientImages.CurrentRow;
if (dgvrow.Cells["DGV_PatientImages_ID"].Value != DBNull.Value)
{
ImgId = Convert.ToInt32(DGV_PatientImages.Rows[RowIndex].Cells[4].Value);
}
any advice please

Well DGV_PatientImages.Rows[RowIndex].Cells[4].Value is a type that can not be cast to int. It is not a string.
By the following line of code, you can simply get what its type is:
string type = DGV_PatientImages.Rows[RowIndex].Cells[4].Value.GetType().ToString();

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
}

Getting System.FormatException: Input string was not in a correct format. when creating a workflow

Creating a custom workflow.
I have an input which is an optionset.
I want to get the text value of this option set that is entered.
Convert it to decimal then add it to my output to populate another decimal field in Dyanimcs
var setvalue = hours.Get<OptionSetValue>(executionContext).ToString();
var hoursDecimal = Convert.ToDecimal(setvalue);
this.decimalval.Set(executionContext,hoursDecimal);
System.FormatException: Input string was not in a correct format. is the error
In your case, using the Decimal.TryParse method with yield the correct result that you want. In your code:
var setvalue = hours.Get<OptionSetValue>(executionContext).ToString();
decimal parsedsetvalue;
if(decimal.TryParse(setvalue,out parsedsetvalue))
{
// success - can use parsedsetvalue
this.decimalval.Set(executionContext,parsedsetvalue);
}
else
{
//error in coverting parsedsetvalue to decimal
return "Error, could not convert setvalue to decimal";
}

c# convert string to int in expression

I'm working on a small project that transforms a list of functions into c# code.
For example: temp1.greaterThan(1); temp2.contains("a");
temp1, temp2 are expression variables in string type.
The source code is:
var temp1 = Expression.Variable(typeof(string), "temp1");
So I think I need to convert temp1 into an integer variable.
I've tried following ways but none worked:
Expression greaterThan = Expression.GreaterThan(temp1, Expression.Constant(1));
It will throw exeption because temp1 is string and hence cannot compare with 1.
Expression.GreaterThan(Expression.Call(typeof(int).GetMethod("Parse"), temp1), Expression.Constant(1));
It throws "Ambiguous match found." exception
Expression.GreaterThan(Expression.Call(typeof(Convert).GetMethod("ToInt32"), temp1), Expression.Constant(1));
Same exception: Ambiguous match found.
Expression.GreaterThan(Expression.Convert(temp1,typeof(Int32)), Expression.Constant(1));
Exception: No coercion operator is defined between types 'System.String' and 'System.Int32'.
So I'm thinking i would need to have a convert method inside the Expression.GreaterThan method.
Anyone has an idea?
Many thanks.
You should use int.Parse to parse a string instead of explicit conversion. Note that int.Parse has a few overloads, that's why you get an "Ambiguous match found" exception.
var temp1 = Expression.Variable(typeof(string), "temp1");
//use int.Parse(string) here
var parseMethod = typeof(int).GetMethod("Parse", new[] { typeof(string) });
var gt = Expression.GreaterThan(Expression.Call(parseMethod, temp1), Expression.Constant(1));

Argument type 'object' is not assignable to parameter type 'string'

I have the following tag in my aspx page
<input type="checkbox" id="chkFlags" name="chkFlags" value="<%#DataBinder.Eval(DataItemContainer, "DataItem.Tag_ID") %>" <%#SelectTags(DataBinder.Eval(Container, "DataItem.Tag_ID")) %>/>
I am getting the following error:
Argument type 'object' is not assignable to parameter type 'string'
The section of the line that is highlighted is:
<%#SelectTags(DataBinder.Eval(Container, "DataItem.Tag_ID"))
My routine for SelectTags is as follows:
public string SelectTags(string Tag_ID)
{
string[] aTags = mobjFormat.StripObjectToString(Request.Form["chkFlags"]).Split(Convert.ToChar(",")); //added square brackets to Request.Form and syntax to convert from string to char 10/21/15 Max //
string sItem = "";
string sReturn = "";
mobjSecurity.MessageStack_Insert("Procedure", "SelectTags", "");
foreach (string sItem_loopVariable in aTags)
{
sItem = sItem_loopVariable;
if (sItem.Trim() == Tag_ID.ToString().Trim()) //added parenthesis to Trim and ToString 10/21/15 Max //
{
sReturn = " checked='checked' ";
}
}
mobjSecurity.MessageStack_Insert("sReturn", sReturn, "SelectTags");
return sReturn;
}
I am not sure what needs to be accomplished to fix this error, perhaps I am just missing something (I have spent many hours looking over this code)
Can someone point me in the direction to take to fix this TIA
As Juharr points out I was just missing the ToString conversion on the line. So I fixed it as follows:
<%#SelectTags(DataBinder.Eval(Container, "DataItem.Tag_ID").ToString())

convert ds.Tables[1].Rows[0].ToString() to int

I can't convert ds.Tables[1].Rows[0].ToString() to an int.
ds.Tables[1].Rows[0] = 100;
Gives an error can't convert.
string test = Convert.Int32(ds.Tables[0].Rows[0]["YourIntegerColumn"].ToString())
If your column isnt an integer, then it's not gonna work, so you'll probly want to check if the column is null, then check if its parsable to an int using Int.TryParse
ds.Tables[1].Rows[0] returns a DataRow. You need to indicate the column that you want to assign the value:
ds.Tables[1].Rows[0]["Your column name"] = value;
Are you missing the column?
ds.Tables[1].Rows[0][column] = value;

Categories