Can't convert values - c#

Im trying to convert the Id field to send it to the DB but in the DB the type is bigInt:
var ID_Inscricao = (Label)row.FindControl("Label1");
How do I convert this to a type that is compatible with bigInt? I have tried cast and convert but neither worked.

You need to convert the value in the label's Text property to an integer of some sort:
string labelText = ((Label)row.FindControl("Label1")).Text;
var ID_Inscricao = Convert.ToInt64(labelText);
Beware that this could throw an exception if the text value is not a number.

You will have to take the Text property and convert that. You can't cast a control to a value.
Hint: Use the appropriate TryParse.

This may help you,
var ID_Inscricao = (Label)row.FindControl("Label1");
var ID_Inscricao_val = parseInt(ID_Inscricao.innerHTML);

Related

Changing the text in a textbox with type date ASP.NET

I am trying to set a text box (which is type = "Date") to a string value which I have taken in from a database.
I have tried the following:
txtDOB.Text = tempRow["DOB"].ToString();
txtDOB.Attributes = tempRow["DOB"].ToString();
But unfortunately I am having no luck with it.
Thanks in advance.
First of all, make sure you have a valid value in tempRow["DOB"].
var dob = Convert.ToDateTime(tempRow["DOB"]);
After that, you can simply:
txtDOB.Text = dob.ToString("MM/dd/yyyy");
txtDOB.Attributes = dob.ToString("MM/dd/yyyy");
Typically this is due to the textbox not understanding the string format of the data you are providing it.
Try changing your code to this:
txtDOB.Text = tempRow["DOB"].ToString("yyyy-MM-dd");
txtDOB.Attributes = tempRow["DOB"].ToString("yyyy-MM-dd");
Validate when debugging (by using the Immediate window of Visual Studio) to see if "tempRow["DOB"].ToString("yyyy-MM-dd")" spits out a valid date string.

input string was not in correct format C#.net

string selected= this.comboBoxitemno.GetItemText(this.comboBoxitemno.SelectedItem);
labelTesting.Text = selected;
int comboBoxitemno_temp = Convert.ToInt32(labelTesting.Text.ToString().Trim());
Still Error shows As follow
From your screenshot, the value of selected is "System.Data.DataRowView". This can't be converted to type int.
So your actual problem is in this line:
this.comboBoxitemno.GetItemText(this.comboBoxitemno.SelectedItem);
Instead of this line, you can use:
`this.comboBoxitemno.SelectedText;`

Attempting to get DASL property value from the Outlook table inbox

I'm attempting to read the DASL value PR_LONGTERM_ENTRYID_FROM_TABLE 0x66700102 mentioned in this thread -
get outlook mailitem for message taken from outlook table
The issue I'm having is with the following line in the code below from the full example below-
string ltEntryid = (string)nextRow["http://schemas.microsoft.com/mapi/proptag/0x66700102".ToString()];
It throws an exception "Cannot convert type 'byte[]' to 'string'"
I might be going about this the wrong way so I'm looking for some advice. I can read all the other tables rows fine (Example- "EntryID(short term), MessageClass, Unread, SenderEmailType).
const string unReadfilter = "[UnRead] = true";
Outlook.Table table = folder.GetTable(unReadfilter, Outlook.OlTableContents.olUserItems);
// Remove the default column set.
table.Columns.RemoveAll();
// Add columns to the table
table.Columns.Add("Unread");
table.Columns.Add("EntryID");
table.Columns.Add("MessageClass");
table.Columns.Add("SenderEmailType");
table.Columns.Add("SenderEmailAddress");
// PR_LONGTERM_ENTRYID_FROM_TABLE
table.Columns.Add("http://schemas.microsoft.com/mapi/proptag/0x66700102".ToString());
// sort table
table.Sort("Unread", true);
while (!table.EndOfTable)
{
Outlook.Row nextRow = table.GetNextRow();
bool unRead = (bool)nextRow["Unread"];
Debug.WriteLine(unRead);
string msgClass = (string)nextRow["MessageClass"];
Debug.WriteLine(msgClass);
string eId = (string)nextRow["EntryID"];
Debug.WriteLine(eId);
string sEaddr = (string)nextRow["SenderEmailAddress"];
Debug.WriteLine(sEaddr);
string sEtype = (string)nextRow["SenderEmailType"];
Debug.WriteLine(sEtype);
// PR_LONGTERM_ENTRYID_FROM_TABLE ***Exception with the following line***
string ltEntryid = (string)nextRow["http://schemas.microsoft.com/mapi/proptag/0x66700102".ToString()];
Debug.WriteLine(ltEntryid);
if (msgClass.Equals("IPM.Note"))
{
//write to string list
dailyMiInboxList.Add(unRead.ToString());
dailyMiInboxList.Add(msgClass);
dailyMiInboxList.Add(eId);
dailyMiInboxList.Add(sEaddr);
dailyMiInboxList.Add(sEtype);
dailyMiInboxList.Add(sEaddr);
dailyMiInboxList.Add(ltEntryid);
}
}
PT_BINARY property is returned as an array of byte, but you are casting it to a string. If you want to convert it to a hex string, use MAPIFolder.PropertyAccessor.BinaryToString().
OK, I figured this out with Dmitry's help.
First when adding this dasl property to the table -
// PR_LONGTERM_ENTRYID_FROM_TABLE
table.Columns.Add("http://schemas.microsoft.com/mapi/proptag/0x66700102".ToString());
I should've not included the tostring so it should be written as follows-
table.Columns.Add(#"http://schemas.microsoft.com/mapi/proptag/0x66700102");
Next in the While loop, to convert the PT_BINARY property from an array of bytes use this to convert the row-
string PR_LONGTERM_ENTRYID_FROM_TABLE = "http://schemas.microsoft.com/mapi/proptag/0x66700102";
string ltEntryId = (string)nextRow.BinaryToString(PR_LONGTERM_ENTRYID_FROM_TABLE);
Debug.Print(ltEntryId);
This link was very helpful
In particular these comments-
• The value returned for a given column representing a binary (PT_BINARY) value depends on whether a built-in property name or a schema name was used to specify the Column. For explicit built-in property names (such as EntryID), the Row(Column) value is returned as a string. For property names referencing a namespace that represent a PT_BINARY property, the Row(Column) value is returned as a byte array. Use Row.BinaryToString to convert the byte array to a string.

Input string was not in a correct format. When i try to convert char->string->int

I have the following code inside my asp.net vmc web application :-
var getNumbers = (from t in ut.newTag
where char.IsDigit(t)
select t).ToString();
tech.PartialTag = Convert.ToInt32(getNumbers);
but i am getting the following exception :-
Input string was not in a correct format.
so can anyone advice how i can solve this issue??
getNumbers is a string, containing type name of string enumerator It will look like
"System.Linq.Enumerable+WhereSelectArrayIterator`1[System.String,System.String]"
You obviously can't convert that type name to integer. If you want to try parse newTag and assign it to PartialTag if there is an integer:
int value;
if (Int32.TryParse(ut.newTag, out value))
tech.PartialTag = value;
There's a ctor of String taking a char[] as parameter, so
var getNumbers = new String((from t in ut.newTag
where char.IsDigit(t)
select t).ToArray());
tech.PartialTag = Convert.ToInt32(getNumbers);
Difference with Sergey's answer :
if your input is 1A2 for example, Sergey's solution won't accept the input.
But my solution (based on yours) will take 12.
So, it depends on what you need (I think Sergey's one is clearer, it just rejects non integers inputs).

Formatting String With LEading Zero's

I am trying to convert an object (coming from a SQL server), into a integer so I can format the number to have the correct amount of zero's in front of it.
For example:
If I were to have 25.6, I would need it to be 0025.6.
Now I have looked online on how to do this, but the methods that I have seen people post are not working for me. I am not entirely sure why. I am trying to format GlobalVariables.grossweightafter. I read the value GlobalVariables.grossweight from the SQL server, but then when I TryParse it, it loses its value. The code I have is below:
while (TransferRecord.Read())
{
//Pulling data from the SQL server. getting data for every line of code as specified.
GlobalVariables.baledate = TransferRecord["keyprinter_datetime"];
GlobalVariables.baleline = TransferRecord["pulp_line_id"];
GlobalVariables.baleid = TransferRecord["bale_id"];
GlobalVariables.grossweight = TransferRecord["bale_gross_weight"];
GlobalVariables.grossweightflag = TransferRecord["gross_value_flag"];
GlobalVariables.baleairdrypercent = TransferRecord["bale_airdry_pct"];
GlobalVariables.airdryflag = TransferRecord["airdry_value_flag"];
//Converting the date, and the baleid to fit in the string.
DateTime.TryParse(GlobalVariables.baledate.ToString(), out GlobalVariables.baledateafter);
int.TryParse(GlobalVariables.baleid.ToString(), out GlobalVariables.baleidafter);
int.TryParse(GlobalVariables.grossweight.ToString(), out GlobalVariables.grossweightafter);
GlobalVariables.grossweightafter.ToString("0000.0");
//Calling the WriteData method.
WriteData();
}
So I was wondering if anyone can catch what I am doing wrong, or they can help me out on the correct way to go about this.
What #Hans Passant was saying is that you need to assign the value returned from .ToString. That line should be:
GlobalVariables.grossweightafter = GlobalVariables.grossweightafter.ToString("0000.0");
The last lines should be
if(int.TryParse(GlobalVariables.grossweight.ToString(), out GlobalVariables.grossweightafter))
{
string grossWeightAfter = GlobalVariables.grossweightafter.ToString("0000.0");
//you need to save the string returned from the ToString-method somewhere or it will be lost.
///Alternatively, if GlobalVariables can contain strings aswell:
GlobalVariables.grossweightafter = GlobalVariables.grossweightafter.ToString("0000.0");
}
else
{
//React on value not being an int
}
Maybe you should try to use double.TryParse() method instead of int.TryParse(), because int does not have fractional part?
Also, you need to store ToString() result to a string variable. Your code should be like this:
GlobalVariables.grossweightafterstring = GlobalVariables.grossweightafter.ToString("0000.0");

Categories

Resources