Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

When I use readonly my insert is throwing a error

Posted on 2008-10-27
6
Medium Priority
?
257 Views
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()
{
       
       document.getElementById("<%=descriptionItemTextBox.ClientID%>").focus();


}
function descriptionItemTextBox_move()
{
       
       document.getElementById("<%=priceTextBox.ClientID%>").focus();


}



//{
//A = document.form1.qtyTextBox.text
//B = document.form1.priceBox.text
//C = (A + B)
//alert("yes");
//document.form1.extendedTextBox.text = eval(A*B)
//}

</SCRIPT>

HERE IS MY TEXTBOX

 <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"
                    Height="19px"></asp:TextBox>


HERE IS MY INSERT THAT IS CALLED IN MY BUTTON_CLICK FUNCTION


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
           
        cnn.Open()
        InsertCommand.ExecuteNonQuery()
        cnn.Close()
        loadLabel.Text = "adf"
        GridView1.DataBind()
       
        descriptionItemTextBox.Text = ""
        qtyTextBox.Text = "0"
        VehicleListBox.SelectedValue = "0"
        extendedTextBox.Text = "0"







0
Comment
Question by:soccerman777
  • 3
  • 3
6 Comments
 
LVL 10

Accepted Solution

by:
jinn_hnnl earned 2000 total points
ID: 22815537
Hi,
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

JINN
0
 

Author Comment

by:soccerman777
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
0
 

Author Closing Comment

by:soccerman777
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

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

Try
    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

JINN

^^
0
 

Author Comment

by:soccerman777
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.
0
 
LVL 10

Expert Comment

by:jinn_hnnl
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?

JINN
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview

971 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