Solved

When I use readonly my insert is throwing a error

Posted on 2008-10-27
6
247 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 500 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
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

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

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