locked
Parsing Error - method class insert parameters RRS feed

  • Question

  • User-952476251 posted

    Hi,

    I don't know if i can ask in this thread,but i have a problem with the code of this method I created(below).

    I debugged the code and it returns to defaults like 0 for the client code and 1/1/0001 12:00:00 AM for the date, I can't figure out if it's a logic or web form problem.
    However, the goal is to display the added deadline data. because the other data is visible via another method I created.
    I am available to anyone who replies to me to add other pieces if they are not understanding.
    Thanks!!!

    Public Sub INSERT_EXP_DATE_TABLE()
        Dim id_customer As Integer
        If Not Integer.TryParse(txt_COD_CUSTOMER.Text, id_customer) Then
            MsgBox("Please enter a valid number.", vbCritical)
            Exit Sub
        End If
    
        Dim exp_date As Date
        If Not Date.TryParse(txt_EXP_DATE.Text, exp_date) Then
            MsgBox("Please enter a valid date.")
            Exit Sub
        End If
    
        Using cn As New SqlConnection("Your connection string"),
                    cmd As New SqlCommand("LST_INSERT_TABLE_01", cn)
            cmd.CommandType = CommandType.StoredProcedure
            With cmd.Parameters
                .Add("@ID_CUSTOMER", SqlDbType.Int).Value = id_customer
                .Add("@COMPANY_NAME", SqlDbType.VarChar, 50).Value = lbl_COMPANY_NAME.Text
                .Add("@EXP_DATE", SqlDbType.Date).Value = exp_date
            End With
            Try
                cn.Open()
                cmd.ExecuteNonQuery()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Using

    Friday, September 4, 2020 7:17 AM

All replies

  • User753101303 posted

    Hi,

    And what are those text values you are trying to parse? TryParse should return false for an empty string but here all happens as if you are ending up with default values. What have you seen exactly when debugging. exp_date is wrong when the SP is called ?

    Not directly related by MsgBox won't work beside your own machine (tries to show a message on the web server itself).

    Friday, September 4, 2020 7:57 AM
  • User-952476251 posted

    I'm trying to convert a string to an integer and string to a date. In the EXP_DATE see 1/1/0001 12:00:00 AM as default value,while in the ID_CUSTOMER see 0 and I need to visualize Textbox values.

    The problem is that they are inside the textboxes, while I am debugging it seems as if it does not save the values ​​inside the textboxes.

    One of the two textboxes are generated automatically if I click a button inside a repeater that displays the cleinte code and company name data (this is displayed inside a label).

    I've searched and asked in any forum but I'm not really stuck on this one.

    I changed my method like this: 

    Public Sub INSErt_EXP_DATE_TABLE()

    MyParm = cmd.Parameters.Add("@ID_CUSTOMER", SqlDbType.Int)

    MyParm.Value = CInt(txt_COD_CUSTOMER.Text)

    MyParm = cmd.Parameters.Add("@COMPANY_NAME", SqlDbType.NVarChar)

    MyParm.Value = lbl_COMPANY_NAME.Text

    MyParm = cmd.Parameters.Add("@EXP_DATE", SqlDbType.Date)

    MyParm.Value = CDate(txt_EXP_DATE.Text)

    Try cmd.Connection = cn

    cmd.CommandText = "LST_INSERT_TABLE_01"

    cmd.CommandType = CommandType.StoredProcedure cmd.Connection.Open()

    cmd.ExecuteNonQuery()

    MsgBox("Date expired conferm", vbInformation)

    Catch ex As Exception

    MsgBox(ex.Message)

    Finally

    cn.Close()

    End Try

    End Sub

    Friday, September 4, 2020 8:22 AM
  • User753101303 posted

    It"s best to understand what happens and then fix the code rather than just trying random changes.

    What I find weird is that :

    Imports System
    
    Module Program
        Sub Main(args As String())
            Dim d As Date
            Console.WriteLine(d) ' default date
            Console.WriteLine(Date.TryParse("", d)) ' returns false
        End Sub
    End Module

    I would really look at which value you have in the textbox for your date and would try to see how Date.TryParse behaves which this value. It seems you should really exit the sub if the value is not valid.

    I would suspect it comes from somewhere else such as your SP or you are looking at older data having this issue?

    For now all happens as if you are using default values while the code seems really to prevent a wrong input using TryParse (which textbox value do you use for testing ?)

    Edit: at worst start with something such as:

    Public Sub INSERT_EXP_DATE_TABLE()
        Dim exp_date As Date
        If Not Date.TryParse(txt_EXP_DATE.Text, exp_date) Then
    lblMessage.Text="Wrong date." ' lblMessage is a Label
    Exit Sub
    End If
    lblMessage.Text=exp_date.ToString()
    End Sub

    Do you see the message or the date. If it works it's liekly your problem is somewhere else than whare you think. Maybe your stored procedure is wrong or depending on how you see later thos values your data retrieval code is wrong???

    Saturday, September 5, 2020 10:24 AM
  • User303363814 posted

    1) Set a breakpoint on the first executable line.

    2) Run the program.

    3) Wait for execution to halt at the breakpoint

    4) inspect all the variables that the line is about to use.  Do they make sense?  If not, stop execution, set a breakpoint earlier in the code, goto step 2

    5) Single step.

    6) Inspect all the variables which should have been changed by the line you just executed.  Do they have the values that you would expect

    7) If you have found an error then stop execution, fix the error. return to step 2 

    8) If you have reached the end of the program - Move to the next task.

    9) Go to 4

    (At step 7, if you cannot fix the problem then tell us which line has the problem, what all the inputs to the line are, what the variables are after execution of the line and what you were expecting.)

    Saturday, September 5, 2020 10:40 PM
  • User-1330468790 posted

    Hi DarkFoxx,

     

    DarkFoxx

    One of the two textboxes are generated automatically if I click a button inside a repeater that displays the cleinte code and company name data (this is displayed inside a label).

    It seems that the problem is located in fetching the value from the text boxes and labels.

    Could you please post the corresponding aspx codes?

    I suspect that you put the text boxes and the labels inside the repeater as well so that you could not fetch the value from them directly using their IDs.

    The correct way should be either using the item event of repeater to get the correct item or using the index to find the correct item of the repeater and, then getting the child controls from the item

    Related links:

    https://forums.asp.net/t/1146008.aspx?Find+control+inside+of+a+repeater+C+

    https://forums.asp.net/t/1297156.aspx?Findcontrol+inside+a+repeater+ItemTemplate

    RepeaterItem Class

     

    Best regards,

    Sean

    Monday, September 7, 2020 2:51 AM