Solved

When I use readonly my insert is throwing a error

Posted on 2008-10-27
6
254 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

627 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