How would i be able to add 2 parameter together?
i have this following code but it dont seem to work
balanceDB = readdata[("balance"+"overdraftLimit")].ToString();
balanceDB = (readdata["balance"] + readdata["overdraftlimit"]).ToString();
This is assuming that you will do the casts. If you don't have casts in your code you need to do something like:
balanceDB = (Convert.ToDouble(readdata["balance"].ToString()) + Convert.ToDouble(readdata["overdraftlimit"])).ToString();
Adjust to fit whatever data type is necessary for these two fields.
Considering that balanceDB is a string. Have you tried this:
balanceDB = readdata["balance"].ToString() + readdata["overdraftLimit"].ToString();
I assume you are trying to do this with a DataReader, right?
You will need to read in each column separately, and concat them together. The code below assumes you are working with strings. If they are integers, or some other type, you need to cast accordingly
balanceDB = readdata["balance"].ToString() + readdata["overdraftlimit"].ToString();
I assume that readdata is a DataTable/datareader and what you want is to add values of balance and overdraftLimit.
int balanceDB;
balanceDB = (Convert.ToInt32(readdata["balance"])+Convert.ToInt32(readdata["overdraftLimit"]));
Supposed the value of balance =1 and overdraftLimit=1 then balanceDB will be equals to 2
Or if you just need to concatenate the two strings:
string balanceDB;
balanceDB = readdata["balance"].ToString()+readdata["overdraftLimit"].ToString();
balanceDB will be equals to 11.
Regards
Related
I have two values, I want to cast these values into a text box. The way I tried is mentioned below
string time="00";
int Result ="02";
textresult.Text=result+' '+time;//first way error result
textresult.Text=(CAST(Result AS varchar(50))+' '+CAST(time AS varchar(50)))//second way..syntax error.
I need output as 02:00 format in text box. Please suggest a way to solve this????
Try this:-
string time = "00";
int Result = 02;
textresult.Text=Result.ToString("D2") + ":" + time;
Try below.
textresult.Text=string.Format("{0}:{1}",Result.ToString("D2"),time);
Your code reports syntax error because there is. It seems you are using C# like sql syntax!
This might help.
string time="00";
int Result =02;
textresult.Text = String.Format("{0}:{1}",Result.ToString("D2"),time);
Fiddle link
I think this should be a pretty easy question to answer but I can't seem to figure it out.
I am adding text to labels from a sqldatasource in c#. All of that works, but I want to be able to format the text. I want to 1) be able to change the format to 0.00 (instead of a string of decimals) and I would also like to be able to add words before the text. I assume I need to somehow use the string.format command but can't figure out how to work it in. Any help would be greatly appreciated. Here's my code below:
DataView dvSql = (DataView)DeskSummary.Select(DataSourceSelectArguments.Empty);
foreach (DataRowView drvSql in dvSql)
{
Desk.Text = drvSql["Deskname"].ToString();
MarginLabel.Text = drvSql["margin"].ToString();
CurrentCI.Text = drvSql["comp_index_primarycomp"].ToString();
WalMartCurrentCI.Text = drvSql["comp_index_walmart"].ToString();
ForecastMargin.Text = drvSql["margin_forecast"].ToString();
WalMartForecastCI.Text = drvSql["comp_index_walmart_forecast"].ToString();
ForecastCI.Text = drvSql["comp_index_primarycomp_forecast"].ToString();
}
You can pass the format argument to the ToString() method like so:
MarginLabel.Text = drvSql["margin"].ToString("0.00");
However, as you said you wanted to prepend some text. Therefore, I recommend:
MarginLabel.Text = String.Format("Prepended text {0:0.00}", drvSql["margin"]);
Note: I just picked one of your labels; I'm not sure which ones get special formatting treatment.
use the
string.Format("This is a before text {"0"},your param)
// you can add as many variables and {""} string literals as you need just make sure that you separate the variables with a ","
Here is the code
string stringNumber = "5123.34214513";
decimal decimalNumber = Decimal.Parse(stringNumber);
string output = String.Format("Your text: {0:0.00}", decimalNumber);
Console.WriteLine(output); //Your text: 5123.34
This works if the column is of type string
String.Format() will do what you need for prepending/appending text values,
string.Format("prepend text {"0"} append text", paramString)
But if you want to actually format the value you are getting back from SQL, then you would need to use String.Format() on that value as well as possibly some RegEx expressions and/or .ToUpperCase or .ToLowercase for your capitalization... something like.
var capitalizedString = paramString.subStr(0,1).ToUppercase + paramString.subStr(1, paramstring.Length);
string.Format("Prepended text {"0"} plus appended text", capitalizedString);
txtBeautified.Text.Remove(txtBeautified.Text.LastIndexOf(","), 1)
i want to find the last index of "," in my text and then remove that , but it is not working. Any Idea? txtBeautified is a richtextbox.
Are you retrieving the result of the operation?
value = txtBeautified.Text.Remove(txtBeautified.Text.LastIndexOf(","), 1)
If you are changing the value of the text box, you need to assign the result back to the text box:
txtBeautified.Text = txtBeautified.Text.Remove(txtBeautified.Text.LastIndexOf(","), 1)
Explanation: Strings cannot be changed. Functions that operate on strings do not change the strings, but return new strings. Therefore, the Remove function returns a string representing the result. To make use of this string, you will need to assign it to a variable/property or pass it into another function call.
Remove is a function. call should be:
txtBeautified.Text = txtBeautified.Text.Remove(txtBeautified.Text.LastIndexOf(","), 1)
Keep in mind that a string is immutable, so the Remove function returns you a new string. You'd need to reassign that new string back to the text box, like:
txtBeautified.Text = txtBeautified.Text.Remove(txtBeautified.Text.LastIndexOf(","), 1);
In this example they are passing only 1 argument. Wat if I wanna pass two ?
When I was using Link Button I was using the two Command arguments like this
Plz check the ItemCommand event in the code above
Now I am clueless how pass 2 args to CheckBox in the repeater! HElp!
followed below example only..its working
http://www.cleancode.co.nz/blog/279/checkbox-repeater-event-handling-argument
concatinate the arguments by a separator then split on it when you want to read them. Something like this:
chk.CommandArgument = obj1.ID + "," + obj2.ID;
string arguments = e.CommandArgument.ToString().Split(",");
int id1 = arguments[0];
int id2 = arguments[1];
You could try the same method as Microgen suggested but use the ToolTip attribute.
I have to deal with a numeric value (coming from a SQL Server 2005 numeric column) which I need to convert to a string, with the caveat that the decimal point has to be removed. The data itself is accessible in C# via a DataTable/DataRow.
For example, if the SQL Server column reads 12345.678, the string should be "12345678" when all is said and done. Would it be possible to point me toward a good approach for this? Maybe there's a .Net conversion method that does this, I'm not sure.
Thanks!
There are several possible approaches. You could convert it to a string using a specific culture so that you are sure that the period is used as decimal separator, then remove the period:
string s = num.ToString(CultureInfo.InvariantCulture).Replace(".", String.Empty);
You could use a more numeric approach to multiply the valye by 10 until there are no decimals:
while (num != Math.Floor(num)) num *= 10.0;
string s = ((int)num).ToString();
what about something like
var numberWithPoint = dbGetNumber();
string numberWithOutPoint = numberWithPoint.ToString().Replace(".", string.Empty);
it's quick and dirty, but it get the job done fairly simply.
you can do it in c# like:
var myNum = 12345.678;
var myString = myNum.ToString().Replace(".","");
in sql server, you can do it like:
SELECT REPLACE( cast(myCol as varchar), '.', '') as FormattedNumber
FROM ...
What about:
// using System.Globalization;
SqlCommand cm = new SqlCommand("SELECT 12345.678 as decimal");
// ...
SqlDataReader dr = cm.ExecuteReader();
if(dr.Read())
{
string value = dr.GetValue(0).ToString()
.Replace(NumberFormatInfo.CurrentInfo.NumberDecimalSeparator, "")
}
but usually when it comes from sql server, you should not convert it first to a string than to integer / double but convert it directly from object in row["column1"] to needed value, this will save you troubles handling cultures and improve performance a bit
A straightforward method is to use
string result = original.Replace(".", "")
There might be a faster method, but if this isn't in a tight inner loop, Replace should work fine.
EDIT: Fixed code example, plus:
To address culture concerns, one might use NumberFormatInfo.NumberDecimalSeparator to determine the currently defined decimal separator.
string result = Regex.Replace(original, "[^0-9]", "");