validate data in excel

milinda nath replied to Sujit Patil at 04-Jul-08 07:11

I have a table called “test” which has the 3 fields id (int), name(string) and age(int)

At the moment I can store the excel sheet values to this table. But I want to check wheather id is an integer,  name is a string ….and so on. And then store these invalid rows in a separate database table called “temptest”. Could you please help me in this regard. Here is my code

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click

 

        If (FileUpload1.HasFile And Path.GetExtension(FileUpload1.FileName) = ".xls") Then

 

            Dim strName As String = Path.GetFullPath(FileUpload1.PostedFile.FileName)

            Dim sSQLTable As String = "test"

 

 

            Dim sWorkbook As String = "[s1$]"

 

            'Create our connection strings

            Dim sExcelConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strName + ";Extended Properties=""Excel 8.0;HDR=YES;"""

            Dim sSqlConnectionString As String = "Data Source=home\SQLEXPRESS;Initial Catalog=db1;User ID=sa; pwd=sa;"

 

            'Execute a query to erase any previous data from our destination table

            Dim sClearSQL = "DELETE FROM " & sSQLTable

            Dim SqlConn As SqlConnection = New SqlConnection(sSqlConnectionString)

            Dim SqlCmd As SqlCommand = New SqlCommand(sClearSQL, SqlConn)

            SqlConn.Open()

            SqlCmd.ExecuteNonQuery()

            SqlConn.Close()

 

            Dim OleDbConn As OleDbConnection = New OleDbConnection(sExcelConnectionString)

            Dim OleDbCmd As OleDbCommand = New OleDbCommand(("SELECT * FROM " & sWorkbook), OleDbConn)

            Dim oledbdadp As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM " & sWorkbook, OleDbConn)

            Dim dt As New DataTable

            oledbdadp.Fill(dt)

 

            OleDbConn.Open()

            If (dt.Rows.Count = 0) Then

                Label1.Text = "No data in the excel sheet"

            Else

 

                Dim dr As OleDbDataReader = OleDbCmd.ExecuteReader()

                Dim bulkCopy As SqlBulkCopy = New SqlBulkCopy(sSqlConnectionString)

                'While (dr.Read())

                '    Dim a As Boolean = IsNumeric(dr(0))

                '    Label2.Text = a.ToString()

                'End While

                bulkCopy.DestinationTableName = sSQLTable

                bulkCopy.WriteToServer(dr)

 

                OleDbConn.Close()

                Label1.Text = "Successfully sent to database"

            End If

 

        Else

            Label1.Text = "Please select a valid file"

        End If

    End Sub

 


Click here to sign in and reply. You could earn money via our $500 contest just for being helpful.
  validate excel data before inserting to sql database - milinda nath  04-Jul-08 06:18 6:18:40 AM
      Try this - Sujit Patil  04-Jul-08 06:30 6:30:17 AM
          validate data in excel - milinda nath  04-Jul-08 07:11 7:11:14 AM
      excel validation - Partha Mandayam  04-Jul-08 06:34 6:34:20 AM
      Try these links - santhosh kumar  04-Jul-08 07:22 7:22:36 AM
      validate - sundar k  04-Jul-08 08:04 8:04:35 AM
      Re : validate excel data before inserting to sql database - Ashutosh Dhok  04-Jul-08 08:06 8:06:41 AM
View Posts