Question Problem with insert method with Texbox

DARK__FOXX

Member
Joined
Sep 2, 2020
Messages
16
Programming Experience
Beginner
Hi,
I asked help for this code that I have created:

VB.NET:
Public Sub INSERISCI_EXP_DATE_TABLE()



        Dim id_customer As Integer
        Dim exp_date As Date

        Try
            cmd.Connection = cn
            cmd.CommandType = CommandType.StoredProcedure
            MyParm = cmd.Parameters.Add("@COD_CUSTOMER", SqlDbType.Int)
            If (Integer.TryParse(txt_COD_CUSTOMER.Text, id_customer)) Then

                MyParm.Value = id_customer
            Else
                MsgBox("customer not found", vbCritical)
            End If
    
            MyParm = cmd.Parameters.Add("@COMPANY_NAME", SqlDbType.NVarChar)
            MyParm.Value = lbl_COMPANY_NAME.Text.ToString
            MyParm = cmd.Parameters.Add("@EXP_DATE", SqlDbType.Date)
            If (Date.TryParse(txt_EXP_DATE.Text, exp_date)) Then

                MyParm.Value = exp_date
            Else
                MsgBox("Exp Date not found", vbCritical)
            End If

            cmd.CommandText = "LST_INSERT_TABLE_01"
            cmd.Connection.Open()
            cmd.ExecuteNonQuery()
            MsgBox("Date registred", vbInformation)
        Catch ex As Exception
            MsgBox(ex.Message)

        Finally
            cn.Close()
        End Try

    End Sub
- Why does my unused variable always return 0 and not the text box value instead?
- How can I return the values found in the textboxes and in the label that will be inserted in the db?

Thanks for help!!!!
 
You need to provide a much better explanation than that. ALWAYS provide a FULL and CLEAR explanation of the problem, which includes what you're trying to achieve, how you're trying to achieve it and what happens when you try. I think I can guess what you're actually trying to say but I shouldn't have to guess or assume. Explain it properly. Have you actually debugged that code? If not, you should done that before posting. If so, you need to explain exactly what you saw.

In a general sense though, what exactly do you expect to happen if either of the inputs fails to parse? You show a message as though it's an error but then you plough on regardless, adding the parameter but not setting its value. That doesn't seem right.

Also, while it's a small thing that doesn't really hurt, what's the point of calling ToString on the result of a Text property, which is type String? How much more of a String do you want it?
 
How can I return the values found in the textboxes and in the label that will be inserted in the db?
Why would you think that it was a good idea to call ExecuteNonQuery to execute a query? If you read up on ADO.NET then you will learn how to read the result set of a query. The point of ExecuteNonQuery is to execute SQL statements that do NOT produce a result set.
 
Finally, you have asked two unrelated questions in this thread:
- Why does my unused variable always return 0 and not the text box value instead?
- How can I return the values found in the textboxes and in the label that will be inserted in the db?
Please keep each thread to a single topic and each topic to a single thread. Each of those questions should be in its own thread, with all and only the information relevant to that question.
 
I debugged the code: clicking on a button located inside a repeater, the customer code and company name label are automatically entered in the text box, but the user only has to enter the date. When I click the save button, I checked the first parameter and it gives me the result 0 instead of giving me the textbox value.
My goal is to save this date in that table and then display it in the repeater.
I tried to search the web but I did not find solutions similar to mine.
 
What Repeater are we talking about? Is this a Web Forms project? If so then you should have posted it in the Web Forms forum. I can move it there if you confirm. Please always post in the most specific forum. This question is not about the .NET Framework at all.
I checked the first parameter and it gives me the result 0
What does that actually mean? Did you actually step through the code? Did Integer.TryParse return True or False? What was the value of txt_COD_CUSTOMER.Text at the time? Either you debugged or you didn't. If you didn't, do it. If you did, provide us with the relevant information. The fact that you haven't done that makes me wonder whether you actually looked for that information yourself. There's no need to "check the parameter" because you can see the data before it goes into the parameter and where it comes from. That's what debugging is for.
 
What Repeater are we talking about? Is this a Web Forms project? If so then you should have posted it in the Web Forms forum. I can move it there if you confirm. Please always post in the most specific forum. This question is not about the .NET Framework at all.

What does that actually mean? Did you actually step through the code? Did Integer.TryParse return True or False? What was the value of txt_COD_CUSTOMER.Text at the time? Either you debugged or you didn't. If you didn't, do it. If you did, provide us with the relevant information. The fact that you haven't done that makes me wonder whether you actually looked for that information yourself. There's no need to "check the parameter" because you can see the data before it goes into the parameter and where it comes from. That's what debugging is for.

It was asp.net project with a repeater that show data in a db and then I have a div with two textbox and a label and a button.
My goal is add exp_date through textbox, save with button, display in the repeater and save on db.
My question is how can I fix this code that I show before.

I'm sorry if I'm not very clear but I'm trying to explain it in the best possible ways
 
Back
Top