When I use readonly my insert is throwing a error

Posted on 2008-10-27
Last Modified: 2012-05-05
whenever I add readonly to my textBox I get a error when I try to do a insert. Any ideas?

Here is my javscript
function priceTextBox_TextChanged()

                A = document.getElementById("<%=qtyTextBox.ClientID%>").value
                B = document.getElementById("<%=priceTextBox.ClientID%>").value
                C = eval(A * B)
                var num = eval(C);
                var price = eval(B);
                var result = num.toFixed(2);
                var resultprice = price.toFixed(2);
                document.getElementById("<%=priceTextBox.ClientID%>").value = resultprice
                document.getElementById("<%=extendedTextBox.ClientID%>").value = result        
                 return true;

function qtyTextBox_move()

function descriptionItemTextBox_move()


//A = document.form1.qtyTextBox.text
//B = document.form1.priceBox.text
//C = (A + B)
//document.form1.extendedTextBox.text = eval(A*B)



 <asp:TextBox ID="extendedTextBox" style="text-align: right;"  runat="server" Width="88px"
                    ToolTip="Total for all of this line item"   Onmouseout="priceTextBox_TextChanged()"  TextMode="MultiLine"


Dim cnn As New SqlConnection("myconnectionstring")
        Dim InsertCommand As SqlCommand = New SqlCommand()
        InsertCommand.Connection = cnn
        Dim sqlInsert As String
        sqlInsert = "INSERT INTO imtbl_templineitem (sessionID, description, unitPrice, extendedAmount, vehicleNumber, qty) VALUES     ("
        sqlInsert += "'" + Session("user") + "',"
        sqlInsert += "'" + descriptionItemTextBox.Text + "',"
        sqlInsert += priceTextBox.Text + ","
        sqlInsert += extendedTextBox.Text + ","
        sqlInsert += VehicleListBox.SelectedValue + ","
        sqlInsert += qtyTextBox.Text + ")"
        InsertCommand.CommandText = sqlInsert
        loadLabel.Text = "adf"
        descriptionItemTextBox.Text = ""
        qtyTextBox.Text = "0"
        VehicleListBox.SelectedValue = "0"
        extendedTextBox.Text = "0"

Question by:soccerman777
  • 3
  • 3
LVL 10

Accepted Solution

jinn_hnnl earned 500 total points
ID: 22815537
I can get, When a control is set to ReadOnly, or Enable = false, it wont be store in ViewState, when the page is posted back, server doesnt retains the control value in the viewstate.

I have this problem when you want to update objectdatasource of the gridview, I want to protect some field which are not DataKey. But I guess they have the same logic.

THe work around is, you can set a hidden field for the corresponding textbox and always set the value of this hidden field as the textbox one when you change (change both). So both are readonly to user

This can be a work around


Author Comment

ID: 22815669
Let me ask you this I trid to caculate the value on the next page like this

Dim newTotal As Double = priceTextBox.Text * qtyTextBox.Text

So I am creating a value Dimed as Double but when I try to insert iusing the insert statment above it throws a error saying  "conversion from string "insert into imtbl_templineitem ("to type 'Double' is not valid

Do you see what the problem with the conversion is. By the way if I put newTotal var that I dimmed above into a text box it outputs the correct amount

Author Closing Comment

ID: 31510474
I actually just set the textfield to read only and recaculated the amount on the post back and got the same amount. I also had to Dim the new var as String for some reason(even through my data type is decimal(8,2) ). But your answer was a just as good.
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

LVL 10

Expert Comment

ID: 22817684
You have to cast them specifically each of the value you got out of the these textbox

    Dim newTotal As Double = Double.Parse(priceTextBox.Text) * Double.Parse(qtyTextBox.Text)
Catch ...
'      ... throw exception
End Try

You have to handle exception here too incase the input is not in double format which can cause the page to crash. Take care of the server culture dependent (such as dot. and comma) to have accurate account ^^

Hope this helps



Author Comment

ID: 22821285
I tried
Dim newTotal As Double = Double.Parse(priceTextBox.Text) * Double.Parse(qtyTextBox.Text)
 but it gave me the same error. For some reason it is forcing me to cast it as string. I realize even though I sent it to my textbox as a decimat that a textbox is going to send it as a string but I am not sure why it wil not let me convert it.
LVL 10

Expert Comment

ID: 22821471
That's odd. Can you try this:
Dim newTotal As Double
newTotal  = CDbl(priceTextBox.Text) * CDbl(qtyTextBox.Text)

You can also try CDec or Double.TryParse

Remember to put these casting inside a Try Catch to handle exception.

Have you debug to see what's the value of the priceTextBox.Text?


Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now