Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have this value field that it inserts inside a new table, but I have two coloumn that they are insert in another table.
This value is inserted by user in a ASP.NET TextBox.
When user click on save button, if this value isn't already inserted, add inside db and in a column table inside a repeater.

Thanks for help!!!

What I have tried:

Public Sub INSERT_EXP_DATE_TABLE()



        Try
            cmd.Connection = cn
            cmd.CommandType = CommandType.StoredProcedure
            MyParm = cmd.Parameters.Add("@CD_CUSTOTMER", SqlDbType.Int)
            MyParm.Value = CInt(txt_CD_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 = txt_EXP_DATE.Text
            cmd.CommandText = "LST_INSERT_TABLE_01"
            cmd.Connection.Open()
            cmd.ExecuteNonQuery()
            MsgBox("Record registered!", vbInformation)
        Catch ex As Exception
            MsgBox(ex.Message)

        Finally
            cn.Close()
        End Try

    End Sub
Posted
Updated 2-Sep-20 6:01am
Comments
CHill60 2-Sep-20 8:28am    
What is the problem with your code?
DARK__FOXX 2-Sep-20 8:31am    
I have a parse issue with date value and I don't know if this procedure it's correct
Afzaal Ahmad Zeeshan 2-Sep-20 8:58am    
Does it fail, and if so, what error does it give?

Also, you did not show the stored procedure code so it might be a little difficult to debug.
DARK__FOXX 2-Sep-20 9:00am    
@CD_CUSTOMER int,
@COMPANY_NAME nvarchar(50),
@EXP_DATE date,
@DTINIT DATETIME
AS

IF EXISTS(SELECT 'True' FROM TABLE WHERE CODE_CUSTOMER = @CODE_CUSTOMER)
BEGIN
SELECT 'FIELD EXIST'
END
ELSE
BEGIN
SELECT 'Record ADD'
INSERT INTO TABLE
(
CD_CUSTOMER,
COMPANY_NAME,
EXP_DATE,
DTINIT
)

VALUES
(

@CD_CUSTOMER,
@COMPANY_NAME,
@EXP_DATE,
@DTINIT
)
END

Look at your code, you are passing invalid data:
SQL
MyParm = cmd.Parameters.Add("@CD_CUSTOTMER", SqlDbType.Int)
MyParm.Value = CInt(txt_CD_CUSTOMER.Text)            // Text is not Int
MyParm = cmd.Parameters.Add("@COMPANY_NAME", SqlDbType.NVarChar)
MyParm.Value = lbl_COMPANY_NAME.Text
MyParm = cmd.Parameters.Add("@EXP_DATE", SqlDbType.Date)
MyParm.Value = txt_EXP_DATE.Text                     // Text is not Date

You declare @CD_CUSTOTMER (are you sure that is the correct spelling?) as SqlDbType.Int and then set its value as a text string. And you do the same with @EXP_DATE. You must convert them (via TryParse) to the proper types first, so that you are passing the correct value types.
 
Share this answer
 
Comments
DARK__FOXX 2-Sep-20 9:28am    
I explain better these values ​​are automatically inserted into a form that I created.
Both ID_CUSTOMER and COMPANY name are values ​​from a table that I joined with a new table where the EXP_DATE column is located.
These values ​​are displayed inside a repeater.
The user must enter only the expiration field, if this field is already there, do not enter at the most, modify the date otherwise enter the expiration date.
Richard MacCutchan 2-Sep-20 9:48am    
Well that does not change the fact that your code is wrong.
DARK__FOXX 2-Sep-20 9:54am    
I'm sorry,I wanted to understand how to try to solve it.
I don't have much knowledge of vb and asp.net
Richard MacCutchan 2-Sep-20 10:00am    
I have told you how to solve it. You must use TryParse to convert the text strings into the proper value types. Until you do that nothing will work.
DARK__FOXX 2-Sep-20 11:13am    
MyParm = cmd.Parameters.Add("@CD_CUSTOMER", SqlDbType.Int)
MyParm.Value = Int32.TryParse(txt_CD_CUSTOMER.Text, unused)
MyParm = cmd.Parameters.Add("@COMPANY_NAME", SqlDbType.NVarChar)
MyParm.Value = lbl_COMPANY_NAME.Text.ToString
MyParm = cmd.Parameters.Add("@EXP_DATE", SqlDbType.Date)
MyParm.Value = DateTime.ParseExact(txt_EXP_DATE.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture)

Tried this solution but doesn't work
In response to OPs comments to Solution 1 ..
MyParm = cmd.Parameters.Add("@CD_CUSTOMER", SqlDbType.Int)
MyParm.Value = Int32.TryParse(txt_CD_CUSTOMER.Text, unused)
MyParm = cmd.Parameters.Add("@COMPANY_NAME", SqlDbType.NVarChar)
MyParm.Value = lbl_COMPANY_NAME.Text.ToString
MyParm = cmd.Parameters.Add("@EXP_DATE", SqlDbType.Date)
MyParm.Value = DateTime.ParseExact(txt_EXP_DATE.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture)
Quote:
tried this solution but doesn't work
I'm not surprised that it doesn't work - see
VB
MyParm = cmd.Parameters.Add("@CD_CUSTOMER", SqlDbType.Int)
MyParm.Value = Int32.TryParse(txt_CD_CUSTOMER.Text, unused)
Int.TryParse() returns a boolean - not a integer. So you can't just assign that to your parameter. Look at the documentation Int32.TryParse Method (System) | Microsoft Docs[^]. The clue is in your variable unused - you haven't used the return value but you should haveThat piece of code should look more like (note not tested)
VB
MyParm = cmd.Parameters.Add("@CD_CUSTOMER", SqlDbType.Int)
If Int32.TryParse(txt_CD_CUSTOMER.Text, unused) Then
    MyParm.Value = unused
Else
    'Let the user know they need to re-enter the data
End If
You've done similar with the txt_EXP_DATE but don't have an out value at all for that - look at the documentation DateTime.TryParseExact Method (System) | Microsoft Docs[^] or have a look at the examples in https://www.dotnetperls.com/datetime-tryparse[^]
 
Share this answer
 
Comments
DARK__FOXX 3-Sep-20 2:50am    
Why does my unused variable always return 0 and not the text box value instead?
CHill60 3-Sep-20 4:12am    
You are probably not handling postback properly and the text boxes are being refreshed by the time you hit this code
DARK__FOXX 3-Sep-20 4:48am    
But this code isn't in a post back inside load_page but in a class that it will call inside a click event
CHill60 3-Sep-20 5:52am    
You need to understand the flow of control when the browser goes back to the server for the OnClick - have a look at Nams article Refresh Page Issue in ASP.Net[^] which might help.
Web coding is not like VB Forms - you need to get your head around what happens at the client end as the page is rendered in the browser, and what is happening at the server end

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900