Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. fail to add data into database...

fail to add data into database...

Scheduled Pinned Locked Moved Visual Basic
databasesecurityhelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zaimah
    wrote on last edited by
    #1

    hi there, im creating a windows form where it will read data in a file n extract the data into the database.. the data in the files are in this format : 20090301,C4933,ASB02 20090301,C4933,BL689 20090301,C4933,BL707 20090301,C4933,BL714 my app will read the data and will put into the table "portal" - date, runNo, coop.. but when i try, theres no data inside the database. But if i try to display all the data from that file, it can be display. so the problem must be insert the data to the database.. pls check my coding if there's a mistake.. TQ..

    Private Sub SEMAK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SEMAK.Click

        Dim fileName As String = "X:\\COOPAYPC1\\01032009\\C4933"
        Dim lineNumber As Integer = 0
    
        Dim sr As StreamReader = New StreamReader(fileName)
        Dim conn As New SqlClient.SqlConnection("Data Source=MISKPI;User ID= sysadm;Password=sysadm;Initial Catalog=SPGA\_Latihan;Persist Security Info=True")
    
        Dim date As String = ""
        Dim runNo As String = ""
        Dim coop As String = ""
        Dim line As String = ""
       
        Do
            line = sr.ReadLine()
    
            If line <> Nothing Then
                Try
                    date= line.Substring(0, 8).Trim()
                    runNo = line.Substring(9, 14).Trim()
                    coop = line.Substring(15).Trim()
    
                Catch ex As Exception
                    Console.Write(ex.ToString())
                End Try
    
                Dim strSQL As String = "INSERT INTO portal (date,runNo, coop) VALUES (@date, @runNo, @coop)"
                Dim cmd As New System.Data.SqlClient.SqlCommand(strSQL, conn)
                conn.Open()
                
                cmd.Parameters.Add(New SqlClient.SqlParameter("@tarikh", SqlDbType.Char, 8))
                cmd.Parameters("@date").Value = date
    
                cmd.Parameters.Add(New SqlClient.SqlParameter("@runNo",   SqlDbType.Char, 20))
                cmd.Parameters("@runNo").Value = runNo
    
                cmd.Parameters.Add(New SqlClient.SqlParameter("@coop",    SqlDbType.VarChar, 100))
                cmd.Parameters("@coop").Value = coop
    
                conn.Close()
                
    
            End If
    
        Loop Until line Is Nothing
    
        sr.Close()
    
    
    End Sub
    
    Y 1 Reply Last reply
    0
    • Z zaimah

      hi there, im creating a windows form where it will read data in a file n extract the data into the database.. the data in the files are in this format : 20090301,C4933,ASB02 20090301,C4933,BL689 20090301,C4933,BL707 20090301,C4933,BL714 my app will read the data and will put into the table "portal" - date, runNo, coop.. but when i try, theres no data inside the database. But if i try to display all the data from that file, it can be display. so the problem must be insert the data to the database.. pls check my coding if there's a mistake.. TQ..

      Private Sub SEMAK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SEMAK.Click

          Dim fileName As String = "X:\\COOPAYPC1\\01032009\\C4933"
          Dim lineNumber As Integer = 0
      
          Dim sr As StreamReader = New StreamReader(fileName)
          Dim conn As New SqlClient.SqlConnection("Data Source=MISKPI;User ID= sysadm;Password=sysadm;Initial Catalog=SPGA\_Latihan;Persist Security Info=True")
      
          Dim date As String = ""
          Dim runNo As String = ""
          Dim coop As String = ""
          Dim line As String = ""
         
          Do
              line = sr.ReadLine()
      
              If line <> Nothing Then
                  Try
                      date= line.Substring(0, 8).Trim()
                      runNo = line.Substring(9, 14).Trim()
                      coop = line.Substring(15).Trim()
      
                  Catch ex As Exception
                      Console.Write(ex.ToString())
                  End Try
      
                  Dim strSQL As String = "INSERT INTO portal (date,runNo, coop) VALUES (@date, @runNo, @coop)"
                  Dim cmd As New System.Data.SqlClient.SqlCommand(strSQL, conn)
                  conn.Open()
                  
                  cmd.Parameters.Add(New SqlClient.SqlParameter("@tarikh", SqlDbType.Char, 8))
                  cmd.Parameters("@date").Value = date
      
                  cmd.Parameters.Add(New SqlClient.SqlParameter("@runNo",   SqlDbType.Char, 20))
                  cmd.Parameters("@runNo").Value = runNo
      
                  cmd.Parameters.Add(New SqlClient.SqlParameter("@coop",    SqlDbType.VarChar, 100))
                  cmd.Parameters("@coop").Value = coop
      
                  conn.Close()
                  
      
              End If
      
          Loop Until line Is Nothing
      
          sr.Close()
      
      
      End Sub
      
      Y Offline
      Y Offline
      Yusuf
      wrote on last edited by
      #2

      well you need to execute your command, don't you?

      cmd.Parameters("@coop").Value = coop
      cmd.ExecuteNonQuery()
      conn.Close()

      Yusuf Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]

      Z 1 Reply Last reply
      0
      • Y Yusuf

        well you need to execute your command, don't you?

        cmd.Parameters("@coop").Value = coop
        cmd.ExecuteNonQuery()
        conn.Close()

        Yusuf Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]

        Z Offline
        Z Offline
        zaimah
        wrote on last edited by
        #3

        yup, tq for the info.. but still after i did the correction there's an error msg come out.. connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) what does this mean?

        C 1 Reply Last reply
        0
        • Z zaimah

          yup, tq for the info.. but still after i did the correction there's an error msg come out.. connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) what does this mean?

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          It means you can't connect to your DB, probably because the connection string is wrong, or because your SQL Server does not allow remote connections.

          Christian Graus Driven to the arms of OSX by Vista.

          Z 1 Reply Last reply
          0
          • C Christian Graus

            It means you can't connect to your DB, probably because the connection string is wrong, or because your SQL Server does not allow remote connections.

            Christian Graus Driven to the arms of OSX by Vista.

            Z Offline
            Z Offline
            zaimah
            wrote on last edited by
            #5

            i dont think its bcoz of sql server does not allow remote connection.. iv been copied this coding from my previous web app.. but the one that im doing currently is windows form.. will it cause a problem? bcoz in web app i will put the connection inside the web.config but in this windows form i put in the coding area as i posted b4. if there's no problem using the coding from my web app, that's why im confused.. my web app can connect to the database, can add, update.. :sigh:

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups