Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi All


VB
Dim strSQL3 As String = "SELECT * FROM PeriodDetails"
Me.DaAp1 = New SqlDataAdapter(strSQL3, con)
Dim Dset1 As New DataSet
DaAp1.Fill(Dset1)




If strSQL3 <> "" Then
    For Each dr In Me.List_Class.SelectedItems
        cmd.CommandText = "INSERT INTO PeriodDetails (Class, Section, Day, Period) VALUES ('" & dr & "','" & List_Section.Text & "','" & MTC1.TodayDate & "','" & cboPeriod.Text & "')"
        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()
    Next
    MsgBox("Recored  Successfully Added")
Else

    For i As Integer = 0 To Dset1.Tables(0).Rows.Count - 1
        cmd.CommandText = ("UPDATE PeriodDetails SET Period='" & cboPeriod.Text & "' WHERE  Class='" & List_Class.Text & "' and Section='" & List_Section.Text & "' and Day='" & CDate(MTC1.SelectionStart) & "'")
    Next i
    con.Open()
    strda = cmd.ExecuteReader()
    con.Close()
    MsgBox("Recored  Successfully Update")
End If

how to use condition for UPDATE and ADD.. What i want to change for above code


Please Tell me How can i do for this code.
Posted
Updated 4-Mar-13 8:29am
v4
Comments
[no name] 4-Mar-13 11:48am    
If (yourValue = 3) Then
update your values
Else
Add new record
Maciej Los 4-Mar-13 11:50am    
Please be more specific and provide more details (example data).
Navas Khanj 4-Mar-13 12:09pm    
above condition ok means i need update Recored

condition not match means Add New Record
[no name] 4-Mar-13 13:02pm    
"If strSQL3 <> "" Then"... strSQL3 is never going to be an empty string since you set it to be something other than an empty string 4 lines above your test. I would suggest that you decide what conditions you are testing for (you have not indicated what that is) and then test for them.

You should try something like the below sample code. The idea behind the code is to iterate all the classes and run the select query with the WHERE clause so you can get the data related to the class, you want to update ( and do not want to insert again). So if there is a row found against ANY class, it means you already have this record so go inside the UPDATE block otherwise go inside the INSERT block.

Here is the sample code: (Please check comments also)

VB
For Each dr In Me.List_Class.SelectedItems  ' check against each class

      Dim strSQL3 As String = "SELECT * FROM PeriodDetails where Class = '" & dr & "' and Section='" & List_Section.Text & "' and Day = '" & MTC1.TodayDate & "' and Period = '" & cboPeriod.Text & "'"
      Me.DaAp1 = New SqlDataAdapter(strSQL3, con)
      Dim Dset1 As New DataSet
      DaAp1.Fill(Dset1)

      ' If there is a table exists in dataset and has NO rows, it means the record DOES NOT exists. Insert it.
      If Dset1.Tables.Count > 0 AndAlso Dset1.Tables(0).Rows.Count = 0 Then
          cmd.CommandText = "INSERT INTO PeriodDetails (Class, Section, Day, Period) VALUES ('" & dr & "','" & List_Section.Text & "','" & MTC1.TodayDate & "','" & cboPeriod.Text & "')"
              con.Open()
              cmd.ExecuteNonQuery()
              con.Close()
          MsgBox("Recored  Successfully Added")
      Else  ' Otherwise update the record with the class you have selected
          For i As Integer = 0 To Dset1.Tables(0).Rows.Count - 1
              cmd.CommandText = ("UPDATE PeriodDetails SET Period='" & cboPeriod.Text & "' WHERE  Class='" & List_Class.Text & "' and Section='" & List_Section.Text & "' and Day='" & CDate(MTC1.SelectionStart) & "'")
          Next i
          con.Open()
          strda = cmd.ExecuteReader()
          con.Close()
          MsgBox("Recored  Successfully Update")
      End If
 Next


Hope it helps!

If you need further assistance please share with us.
 
Share this answer
 
v3
Comments
Navas Khanj 5-Mar-13 2:50am    
Hi.. above code New Record Add function only working UPDate not working.. pls tell me some idea..
Navas Khanj 5-Mar-13 3:37am    
thanks for your Replay..
Shahan Ayyub 5-Mar-13 6:43am    
Is your issue resolved ? If no, what is unexpected to you when running a case of UPDATE ?
Navas Khanj 5-Mar-13 9:23am    
yes dear.. my Issue is Solve. your code help for my issue
Shahan Ayyub 5-Mar-13 9:46am    
may I ask why a down vote, if it helps ? did you do that ?
Hi Guys,

I am new in asp.net. as a beginner, i created a sample welcome page and save it in .htm. however, when i try to publish it using IIS in windows 7 64 something went wrong. see below for error message:

RockMelt's connection attempt to localhost was rejected. The website may be down, or your network may not be properly configured.

Hope someone could help me.

thanks in advance!
 
Share this answer
 

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