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;
Related
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();
DataRow drEmpty = dsResult.Tables[1].NewRow();
dsResult.Tables[1].Rows.InsertAt(drEmpty, 0);
DataRow drBranch = dsResult.Tables[1].Rows[1];
drBranch[1] = "Branch"; <--error
dsResult.Tables[1].Rows.InsertAt(drBranch, 1);
my expected output for this few line of code is to add first empty row data into my dropdownlistbox and second row by adding "Branch" into the it but i failed to do so
Error msg given --
Input string was not in a correct format.Couldn't store in ows_ID Column. Expected type is Int64.
after that i try to change to
drBranch[1] = int64.Parse("Branch");
and i get another error
second error msg -- Input string was not in a correct format.
i get the answer that i want already, i will post the answer after 7 hours ,thanks all
In you example drBranch[1] represents the ows_ID column, which only accepts values of type integer. That means only numbers without a decimal point are allowed.
You could convert strings that contain numbers like "55" to an integer using Int64.Parse("55"), but you can't convert strings like "Branch" to integer. You're trying to store a string in the ows_ID column. Maybe you're trying to access the wrong index?
The problem is that "Branch" is not integer.
To cast a value into integer you need an integer value as a string like "526", "100".
I assume you think that the column ordinal is one-based but it's zero-based, hence drBranch[1] is the second column.
In my opinion it's better to use the column's name instead:
drBranch.SetField<String>("Branch", newBranch);
(i'm using the SetField extension method since it's strongly typed and supports nullables)
I would suggest using a DataTable before using DataRow:
DataTabel dtResult = dsResult.Tables[1].Clone();
dtResult.Columns[1].DataType = Type.GetType("System.String");
DataRow drEmpty = dtResult.NewRow();
dsResult.Tables[1].Rows.InsertAt(drEmpty, 0);
DataRow drBranch = dsResult.Tables[1].Rows[1];
drBranch[1] = "Branch";
dsResult.Tables[1].Rows.InsertAt(drBranch, 1);
I am tring to return an int value with comma seperators within the value.
12345 would be returned as 12,345
The follwing code works:
int myInt = 1234567;
MessageBox.Show(string.Format("My number is {0}", myInt.ToString("#,#")));
12,345 is displayed as expected.
While the following code does no work, but from what I am reading, should work:
int myInt = 1234567;
MessageBox.Show(string.Format("My number is {0:#,#}", myInt.ToString()));
12345 is displayed.
Can you help me understand why the second set of code is not working?
Thanks
You shouldn't ToString the int before the format. Try this:
MessageBox.Show(string.Format("My number is {0:#,#}", myInt));
You are converting to string before the formatter has the chance to apply the formatting. You are looking for
MessageBox.Show(string.Format("My number is {0:#,#}", myInt));
myInt.ToString() is redundant since you are using a String.Format(). The point of String.Format is to supply is with a bunch of objects and it will create a string for you. No need to convert it to a string.
In order for the Numeric Format to reflect you need to give it an actual numeric value type not a numeric value that's of type string.
When you give the Format method a string it doesn't take into account any numeric formatting since its already a string type.
int myInt = 1234567;
MessageBox.Show(string.Format("My number is {0:#,#}", myInt));
When you use format strings "inline" as in your second example, the input to the parameter needs to be the original value (i.e int, double, or whatever) so that the format string can do something useful with it.
So omitting the ToString from the second call will do what you want:
int myInt = 1234567;
MessageBox.Show(string.Format("My number is {0:#,#}", myInt));
hello mates i am trying to store value from dropdown list to an integer but i am getting an exception Input string was not in a correct format.
int experienceYears = Convert.ToInt32("DropDownList1.SelectedValue");
please help.
Remove the quotes; the code as it stands is trying to convert the literal string "DropDownList1.SelectedValue" to an integer, which it can't.
int experienceYears = Convert.ToInt32(DropDownList1.SelectedValue);
Try it without the quotes:
int experienceYears = Convert.ToInt32(DropDownList1.SelectedValue);
I'm trying to dynamically set an enum based on the value in a string so far so good I don't know what I've been doing wrong. I have the following code:
public enum TagLabels : long
{
TurnLeft = 0x0000000000000000000030A38DB1,
TurnRight = 0x00000000000000000000307346CC,
LiftApproach = 0x0000000000000000000012107A8D
}
TagLabels IDs;
string someID = "0x0000000000000000000012107A8D";
IDs = (TagLabels)Enum.Parse(typeof(TagLabels), someID ); //<== I get runtime error on this line
I cannot see what's wrong with what I'm doing.
Enum.Parse is intended to convert a string representation of the symbolic name into an enum val, as in Enum.Parse("TurnLeft"). If what you have is a string giving the numeric value, then you should just parse the string as the corresponding integer type and cast it to the Enum val.
IDs = (TagLabels)long.Parse("0x0000000000000000000012107A8D");
IDs = (TagLabels)Convert.ToInt64(someID, 16);
EDIT: You have a string that is in hex format and not a direct number. So, it needs conversion to int first.
If the Enum value exists, you can cast an int value to Enum type.
EDIT2: Changed after Marc's suggestion from Convert.ToInt32 to Convert.ToInt64
SomeID is a string and your enum is a long.
Try using TurnLeft instead of "0x0000000000000000000012107A8D"
Where is the string you're parsing? Aren't you trying to turn a string like "TurnLeft" into TagLabels.TurnLeft?
MSDN