method insert -problem parsing value with parameters

DARK__FOXX

Member
Joined
Sep 2, 2020
Messages
9
Programming Experience
Beginner
Hi,

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).

This is my code
VB.NET:
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
 
Last edited by a moderator:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,319
Location
Sydney, Australia
Programming Experience
10+
One of the reason we use code formatting tags on sites like this is because they respect indenting, which vanilla HTML does not. To post code with no indenting defeats the purpose and makes it hard for us to read your code. Please make the effort to do all you can to help us help you, which would include making your code as readable as possible.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,319
Location
Sydney, Australia
Programming Experience
10+
Speaking of readable code, this:
VB.NET:
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)
could be more succinctly written like this:
VB.NET:
cmd.Parameters.Add("@ID_CUSTOMER", SqlDbType.Int).Value = CInt(txt_COD_CUSTOMER.Text)
cmd.Parameters.Add("@COMPANY_NAME", SqlDbType.NVarChar).Value = lbl_COMPANY_NAME.Text
cmd.Parameters.Add("@EXP_DATE", SqlDbType.Date).Value = CDate(txt_EXP_DATE.Text)
Either way, you should be specifying the size of a text parameter as well as the type.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,319
Location
Sydney, Australia
Programming Experience
10+
while I am debugging it seems as if it does not save the values inside the textboxes.
There is no "seems". The whole point of debugging is to see exactly what happens. Set a breakpoint at the top of that code, step through it and actually examine the state at each step. You can actually see what the parameter values are with your own eyes, so there's no need to guess. If the parameter values are what they should be then obviously the issue is in the SQL code that uses those parameter values and any question regarding the VB code is moot. If the parameters aren't what you expect then you debug that code to find out why, which would start by actually looking at the contexts of the TextBoxes. You need to work out what problem you're actually trying to solve before you try to solve it and that's what you use the debugger for.
 

DARK__FOXX

Member
Joined
Sep 2, 2020
Messages
9
Programming Experience
Beginner
I debugged the code, the values inside the text box are not compatible with the debug values. They are empty fields.
The compiler gives me an exception cast error in the client_code parameter and stops execution.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,319
Location
Sydney, Australia
Programming Experience
10+
One of the big problems I see with people trying to solve problems is that they have no clue what problem they are actually trying to solve and, thus, have no hope of solving it.
I debugged the code, the values inside the text box are not compatible with the debug values. They are empty fields.
Are you actually saying that here:
VB.NET:
MyParm.Value = CInt(txt_COD_CUSTOMER.Text)
the value of txt_COD_CUSTOMER.Text is an empty String? If so then what are you actually asking for? Are you saying that that control is empty but you don't think it should be? Are you asking how to successfully execute the stored procedure even if no value is provided? Are you asking something else? I have no idea from the information you have provided.
The compiler gives me an exception cast error in the client_code parameter and stops execution.
I have no idea what that means because there's no mention of "client_code" anywhere in your previous posts. If you provide a FULL and CLEAR explanation of the problem at the start - this is EXACTLY what I'm trying to achieve, this is EXACTLY how I'm trying to achieve it and this is EXACTLY what happens when I try - then we don't have to try to get the full picture by dragging all the information out bit by bit. Too many people try to get away with providing as little information as possible. Assume that too much information is better than not enough. For instance, you seem to be suggesting that an exception is thrown but have failed to provide the specific code that throws the exception, exactly where it occurs and the actual error message. If you would like us to help you then take it upon yourself to help us, however much effort and time might be involved in that. We don't want to keep going over and over the same threads, asking for information that you should have already provided. Eventually, people will lose interest and you'll get no help at all.
 
Top Bottom