how do I convert textbox value to string? - asp.net

I know this is simple but i am lost as to how to approach it. Iam a new newbie. Please have it on me as i am new here.
I have a textbox field called ClientsBalance.
This balance can is usually in money order with a set amount. The client is also allowed to pay by Debit Cards.
Here goes:
Dim paymentType As TextBox
otherPyment As String = "Debit Card"
If paymentType.Text <> "1250" Then
paymentType = "OtherPayment"
else
paymentType = gridview1.FindControl("paymentType" & CStr(1))
end if
Everything was working before the validation.
Now, I get the following error:
Value of type 'String' cannot be converted to 'System.Web.UI.WebControls.TextBox'
Is there a way I can cast this line:
paymentType = "OtherPayment"
??
Thank you so much experts.

you need to say
paymentType.Text = "OtherPayment".
You just left out the ".Text"

Try changing this:
paymentType = "OtherPayment"
To this:
paymentType.Text = "OtherPayment"

Related

need help update a datatable cell value

I have a trivial question about updating a datatable MyDT. I googled and found several approach and got compile errors. Here is the code and here is what I tried with the error. Any help is greatly appreciated. BTW, I am using asp.net framework 2.0 and VB.NET
MyDT.Rows[1][4] = "4NF" ' Property access must assign to the property or use it value
row.Item("New_Column") = "4NF" ' Input string was not in a correct format.
Couldn't store <4NF> in New_Column Column. Expect type is Byte.
row["New_Column"] = "4NF" ' Expression is not a method
Dim StatusCode As String
For Each row As DataRow In MyDT.Rows
StatusCode= row.Item("ThisColumn").ToString()
If StatusCode= "NONF" Then
MyDT.Rows[1][4] = "4NF"
End If
Next row
You should either:
Store data as String in your DataTable
Do a conversion
Here how the conversion looks
MyDT.Rows[1][4] = Convert.ToByte("4NF")

Textbox dont allows null value in ASP .Net

i have column in database with Int data type, but textbox don't allows null. it gives error "Input string was not in a correct format".
objinsert.VehGrpID = Convert.ToInt32(txtVehGroupID.Text);
Use TryParse instead.
int GrpID = 0;
int.TryParse(txtVehGroupID.Text, out GrpID)
if(GrpID > 0)
objinsert.VehGrpID = GrpID;
You can just special-case out empty values. I am, of course, assuming here that an empty value should map to null.
objinsert.VehGrpID = string.IsNullOrWhiteSpace(txtVehGroupID.Text) ? null : (int?)Convert.ToInt32(txtVehGroupID.Text);
You'll also want to adjust your code to set the textbox's text accordingly.
Your question is also a bit unclear, because you say the textbox won't allow nulls, but you haven't shown us any code that adjusts the textbox text.
You can also use DBNull.Value Field. If a database field has missing data, you can use the DBNull.Value property.
check this link http://msdn.microsoft.com/en-us/library/system.dbnull.value%28v=vs.110%29.aspx
try this
int VehGrpID = Convert.ToInt32(txtVehGroupID.Text);
OR
int VehGrpIDtxtVehGroupID.Text = int.Parse(txtVehGroupID.Text);
The Text property of your textbox is a String type, so you have to perform the conversion in the code.

how do i delete characters in a string/text of a textbox?

I have an asp.net 4 textbox control that has it's text being dynamically populated by some java script. A Google Maps call to be exact. It's giving me mileage from 1 point to another. When the text displays, it shows " 234 mi" I need to get rid of the "mi" part of this text because the text is being converted to an Int32 Updating a table in my DB.
Basically I can only have an INT. Nothing else in the text box. How do I get rid of the "mi" at the end of the text?
Thanks
C#
EB
On the postback, before you save it you could:
var saveValue = Int32.Parse(tbTarget.Text.Replace("mi", string.Empty).Trim());
If your working with a variable length of chars (say someone enters miles instead) then your must do a foreach against the string (an array of char) and check isnumeric on each char.
A simple String.Substring works also:
String leftPart = TxtMileAge.Text.Substring(0, txt.IndexOf(' '));
int mileAge = int.Parse(leftPart);
This retrieves the part of the String in the range of 0 - indexOfWhiteSpace and converts it to an int
Edit: Since the value can have decimal places (as you've commented), you need to parse it to double, round it and then cast it to int:
var txtEstDistance = new TextBox() { Text = "40.2 mi" };
String leftPart = txtEstDistance.Text.Substring(0, txtEstDistance.Text.IndexOf(' '));
double distanceMiles = double.Parse(leftPart, System.Globalization.CultureInfo.InvariantCulture);
int oDdstanceMiles = (int)Math.Round(distanceMiles, MidpointRounding.AwayFromZero);

Object Data Source

I'm creating a gridview using an objectdatasource and it works fine when pulling all records. But when I want to use the selectCountMethod the grid shows no values.
I Step through the code and my getInvoices (gets the requested data) returns data and the getInvoicesCount (gets the total record count). But then when I go through the rowdatabound of the gridview there's nothing in there and no data displays.
Here is my code to set the objectdatasource. Any reasons why it wouldn't work or something special that needs to be done for getting the selectcount to work?
Me.ODS.TypeName = "invoice"
Me.ODS.EnablePaging = True
Me.ODS.SelectMethod = "getInvoices"
Me.ODS.SelectCountMethod = "GetInvoiceCount"
Me.ODS.StartRowIndexParameterName = "startRowIndex"
Me.ODS.MaximumRowsParameterName = "maximumRows"
Me.ODS.SelectParameters.Add("strbu", strBusUnit)
Me.ODS.SelectParameters.Add("stremailAddress", emailAddress)
Me.ODS.SelectParameters.Add("startDate", search_startdate)
Me.ODS.SelectParameters.Add("enddate", search_enddate)
Me.ODS.SelectParameters.Add("sortExpression", sortExpression & " " & sortDirection)
With gvInvoices
.PageIndex = intPageIndex
.PageSize = 25
.DataBind()
End With
Check if the count being returned is an integer . debug it . maybe it is null.
and if not null parse it to an integer
I was able to figure this one out. The count was being returned as a long instead of integer. I changed it to integer and all is working great

how to add 2 values of textbox in asp.net using javascript

I have 2 textbox in my asp.net page and also have one hiddenfield in my asp.net page , my hiddenfield will always have numeric value like 123.00 , and in my one textbox also I will always have numeric value like 20.00 now I want to add this hiddenfield value and textbox value and display it into second textbox thru javascript
I wrote the following code to do this
var amt = document.getElementById("txtsecond");
var hiddenamt = document.getElementById("AmtHidden").value
var fee = document.getElementById("txtFirst").value;
amt.value = hiddenamt + fee;
this should give me result like 123.00+20.00 = 143.00 but this is concatnating hiddenamt value and fee value and giving me result like 12320.00 in my first textbox
can anybody suggest me what is wrong in my code and what is the right way to get desired value
amt.value = parseFloat(hiddenamt) + parseFloat(fee);
the value of an input is just a string - convert to float parseFloat(foo) in JS and you'll be fine
edited to make float as I notice it's probably important for you
Textboxes are strings, you need to convert from a String to a Number:
var hiddenamt = parseFloat(document.getElementById("AmtHidden").value);
var fee = parseFloat(document.getElementById("txtFirst").value);
Eric
you should parse the values to decimals first:
decimal amt, hiddenamt, fee;
Decimal.TryParse(document.getElementById("txtsecond"),out amt);
Decimal.TryParse(document.getElementById("txtfirst"),out fee);
hiddenamt = amt + fee;

Resources