Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a code that I input data inside my SQL, here is the code :
<pre>   
Dim db As ADODB.Connection
        Dim connstr As String
        Dim adoRS As ADODB.Recordset
        Dim sqlstr As String


        If (DateTimePickerFixCosts1.Text = "" Or Val(DateTimePickerFixCosts1.Text) = 0) Then
            Call MsgBox("Please insert Date")
            Exit Sub
        End If
        If (TxtIDFixCosts.Text = "") Then
            TxtIDFixCosts.Text = ""
        End If


        If (TxtItemDescFixCosts.Text = "") Then
            TxtItemDescFixCosts.Text = 0
        End If
        If (TxtPaymentFixCosts.Text = "") Then
            TxtPaymentFixCosts.Text = 0
        End If
        If (txtPriceFixCosts.Text = "") Then
            txtPriceFixCosts.Text = 0
        End If


        If (txtNumberofMonths.Text = "") Then
            txtNumberofMonths.Text = 0
            Exit Sub
        End If


        db = New ADODB.Connection
        db.ConnectionTimeout = 10
        connstr = "Provider = sqloledb;Data Source=ELVIS\SQLEXPRESS;Initial Catalog=Expenses;Trusted_Connection=yes;"
        db.ConnectionString = connstr
        db.Open()
        sqlstr = "Select * from Test_Table where ID = '" & Val(TxtIDFixCosts.Text) & "'"
        adoRS = New ADODB.Recordset
        adoRS.Open(sqlstr, db, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)

        If (adoRS.EOF = True) Then
            ' insert
            adoRS.AddNew()
            adoRS.Fields("Date").Value = DateTimePickerFixCosts1.Value

            adoRS.Fields("Department").Value = TxtFixCosts.Text
            adoRS.Fields("Description").Value = TxtItemDescFixCosts
            adoRS.Fields("Payment").Value = TxtPaymentFixCosts.Text
            adoRS.Fields("Price").Value = txtPriceFixCosts.Text
            MsgBox("Your Record has been inserted")
            adoRS.Update()

        Else
            If (MsgBox("This item in this date is already inserted " & vbCrLf & "Would you like to overwrite it?", MsgBoxStyle.YesNo, "This Item exist") = vbYes) Then
                ' overwrite
                adoRS.Fields("Date").Value = DateTimePickerFixCosts1.Value
                adoRS.Fields("Department").Value = TxtFixCosts.Text
                adoRS.Fields("Description").Value = TxtItemDescFixCosts.Text
                adoRS.Fields("Payment").Value = TxtPaymentFixCosts.Text
                adoRS.Fields("Price").Value = txtPriceFixCosts.Text

                MsgBox("Your data has been overwrited")
                adoRS.Update()
            End If
        End If
        adoRS.Close()
        db.Close()
The field down is what I tried, I also tried to connect them but no success, if there is someone who can help me I would be very greatful.

What I have tried:

' Dim originDate As DateTime = DateTime.Parse(DateTimePickerFixCosts1.Text)
       ' Dim result1 As DateTime = originDate.AddMonths(MonthssToAdd)
       ' MessageBox.Show(result1.ToLongDateString())
Posted
Updated 12-Jul-20 22:30pm
Comments
Maciej Los 13-Jul-20 4:14am    
Do you use VB.NET or VB?
If it's VB.nET, please stop using ADODB! Use ADO.NET instead.
Member 13410460 13-Jul-20 4:16am    
Thank you for your answer, it is VB:NET and yes i do use ADODB because i am more familiar to it.
Laxmidhar tatwa technologies 13-Jul-20 11:31am    
Dim result1 As DateTime = DateTimePickerFixCosts1.Value.AddMonths(1)

Try this:
VB.NET
Dim originDate As DateTime = DateTimePickerFixCosts1.Value
Dim result1 As DateTime = originDate.AddMonths(MonthssToAdd)


BTW:
1) Do not use ADODB within VB.NET. Use ADO.NET[^] instead!
2) Do not use concatenated text to build query. Use parameterized queries[^] instead, because your code is Sql Injection[^] vulnerable!

Finally:
VB.NET
Dim dt As DataTable = New DataTable()
Try
    Using connection As New SqlConnection("ConnectionStringHere")
        conection.Open()
        Using command As New SqlCommand("commandTextHere", connection)
            Using reader As SqlDataReader = command.ExecuteReader()
                dt.Load(reader)
            End Using
        End Using
    End Using
Catch Ex As Exception
    Console.WriteLine(Ex.Message)
End Try
If dt.Rows.Count=0 Then
    'something went wrong!
End If


Good luck!
 
Share this answer
 
Comments
Member 13410460 13-Jul-20 4:31am    
Thank you for your Response Maciej Los, i will try this and let you know the results.
Bests
Maciej Los 13-Jul-20 4:32am    
You're very welcome!
Member 13410460 13-Jul-20 4:53am    
Hi again, I did some Research on what you said and I saw the 'parameterized queries' uses SQL commands which I am also not so good, I know the Basics but I dont know how to connect the 5 values that Need to be inserted with the textfield of numbers of ('result1').
Maciej Los 13-Jul-20 5:03am    
Can you be more specific? I don't get this part: "how to connect the 5 values that Need to be inserted with the textfield of numbers of ('result1')."
Member 13410460 13-Jul-20 5:14am    
Sorry if I am not clear English is not my native language, So as I looked into the codes i figure it out how could i've done and this is the Code (sqlcommand) :
string sqlText = "Insert into Expenses " +
"SET Description = '" + txtItemDescFixCost.Text + "', " +
"SET Payment = '" + txtPaymentFIxCost.Text + "', " +
"SET Price = '" + txtPriceFixCost.Text + "', " +
"Date = " + datetimepicker1.Text + " " +
"WHERE ID = " + Val(txtID.text);
And now in this part where or how should I insert the "result1".

Again thank you for your patiance with me.
I don't know exactly what you are looking for but with this "then i tried to use a textfield which i insert 12( as 12 months) and to record all those values 12 times for one year. ", am giving you a solution that may meet your needs else let me know.
VB
For i = 1 To 12
  'Place your insertion codes here
Next

This will cause your code to execute 12 times cos according to you, you want to save the values 12 times
 
Share this answer
 
Comments
Member 13410460 13-Jul-20 4:30am    
Thank you for your asnwer Mr.Emmanuel, this will record datas 12 times in the same date in my database, I want to record them Monthly for exmaple;
On 01.January.2015 all the 5 records to be inserted then
On 01.February.2015 again all the 5 records to be inserted and so on so on for 5 years.
Avoid data Format as I took it only for example.

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