Set the Default Value to the Column Of a Table Programmaticaly

Normally we face this type of problem when we want to set the column default value pro-grammatically.

To Solve this problem we need to first get the table column information using this Query.

"SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='person'".

Here the table name may vary.

when we execute this Query we will get the each column information .So based on this we can Iterate the each column and get the each column Information as given below 
 
 

 
VB.NET 
 Private Sub Initialise_The_Table(ByVal ReqTab As String)  
      On Error GoTo handle_error  
      Dim eachTableAdapter As SqlDataAdapter  
      Dim tableDataset As New DataSet  
      Dim adoHelper As SQLHelper = SQLHelper.GetInstance  
      eachTableAdapter = adoHelper.GetAdapter("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" + ReqTab + "'")  
      eachTableAdapter.FillSchema(tableDataset, SchemaType.Source, ReqTab)  
      eachTableAdapter.Fill(tableDataset, ReqTab)  
      For ii As Integer = 0 To tableDataset.Tables(ReqTab).Rows.Count - 1  
        If Convert.ToString(tableDataset.Tables(ReqTab).Rows(ii)("DATA_TYPE")).ToLower = (SqlDbType.NVarChar.ToString.ToLower) _  
        Or Convert.ToString(tableDataset.Tables(ReqTab).Rows(ii)("DATA_TYPE")).ToLower = SqlDbType.NText.ToString.ToLower _  
        Or Convert.ToString(tableDataset.Tables(ReqTab).Rows(ii)("DATA_TYPE")).ToLower = SqlDbType.Int.ToString.ToLower _  
        Or Convert.ToString(tableDataset.Tables(ReqTab).Rows(ii)("DATA_TYPE")).ToLower = SqlDbType.Real.ToString.ToLower _  
        Or Convert.ToString(tableDataset.Tables(ReqTab).Rows(ii)("DATA_TYPE")).ToLower = SqlDbType.Float.ToString.ToLower _  
        Or Convert.ToString(tableDataset.Tables(ReqTab).Rows(ii)("DATA_TYPE")).ToLower = SqlDbType.SmallInt.ToString.ToLower _  
        Or Convert.ToString(tableDataset.Tables(ReqTab).Rows(ii)("DATA_TYPE")).ToLower = SqlDbType.TinyInt.ToString.ToLower _  
        Or Convert.ToString(tableDataset.Tables(ReqTab).Rows(ii)("DATA_TYPE")).ToLower = SqlDbType.Money.ToString.ToLower Then  
          If Convert.ToString(tableDataset.Tables(ReqTab).Rows(ii)("DATA_TYPE")).ToLower = SqlDbType.NVarChar.ToString.ToLower _  
          Or Convert.ToString(tableDataset.Tables(ReqTab).Rows(ii)("DATA_TYPE")).ToLower = SqlDbType.NText.ToString.ToLower Then  
            If Convert.ToString(tableDataset.Tables(ReqTab).Rows(ii)("IS_NULLABLE")).ToLower = "yes" Then ''''Checking the column IS_NULLABLE   
              If Convert.IsDBNull(tableDataset.Tables(ReqTab).Rows(ii)("COLUMN_DEFAULT")) Or _  
              String.IsNullOrEmpty(tableDataset.Tables(ReqTab).Rows(ii)("COLUMN_DEFAULT").ToString) = False Then  
                Errorlog.WriteLog(Information.Err().Number, "Field Defaults set on ", ReqTab, _  
                         tableDataset.Tables(ReqTab).Rows(ii)("COLUMN_NAME"))  
                adoHelper.ExecNonQuery("ALTER TABLE [dbo].[" + ReqTab + "] ADD CONSTRAINT [DF_" + ReqTab + "__" + _  
                         tableDataset.Tables(ReqTab).Rows(ii)("COLUMN_NAME") + "] DEFAULT '' FOR " + _  
                         tableDataset.Tables(ReqTab).Rows(ii)("COLUMN_NAME") + "")  
                GoTo Next_Field  
              End If  
            End If  
          Else  
            If Convert.ToString(tableDataset.Tables(ReqTab).Rows(ii)("IS_NULLABLE")).ToLower = "yes" Then ''''Checking the column IS_NULLABLE   
              If Convert.ToString(tableDataset.Tables(ReqTab).Rows(ii)("COLUMN_DEFAULT")) <> "((0))" Then  
                If Convert.ToString(tableDataset.Tables(ReqTab).Rows(ii)("COLUMN_DEFAULT")).ToLower() <> "genuniqueid()" _  
                And Strings.Mid(Convert.ToString(tableDataset.Tables(ReqTab).Rows(ii)("COLUMN_NAME")), 1, 2).ToLower() <> "s_" Then  
                  Errorlog.WriteLog(Information.Err().Number, "Field Defaults set on ", ReqTab, _  
    tableDataset.Tables(ReqTab).Rows(ii)("COLUMN_NAME"))  
                  adoHelper.ExecNonQuery("ALTER TABLE [dbo].[" + ReqTab + "] ADD CONSTRAINT [DF_" + ReqTab + "__" + _  
                             tableDataset.Tables(ReqTab).Rows(ii)("COLUMN_NAME") + "] DEFAULT 0 FOR " + _  
                             tableDataset.Tables(ReqTab).Rows(ii)("COLUMN_NAME") + "")  
                End If  
                GoTo Next_Field  
              End If  
            End If  
          End If  
        End If  
  Next_Field:  
      Next ii  
      Exit Sub  
  handle_error:  
      If Information.Err().Number = 3078 Then  
        MessageBox.Show("No such table in database", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)  
        Exit Sub  
      Else  
        If Do_All = 1 And Not Init_Phase Then  
          MessageBox.Show(Information.Err().Description, "System Error: " & Information.Err().Number & " table = " & Public_db.TableDefs(i).Name, _         MessageBoxButtons.OK, MessageBoxIcon.Error)  
        Else  
          MessageBox.Show(Information.Err().Description, "System Error: " & Information.Err().Number, MessageBoxButtons.OK, MessageBoxIcon.Error)  
        End If  
      End If  
      Resume  
    End Sub  





I think solution will be helpful to many guys :-)
Happy meaningful day :-)

Popular posts from this blog

Exporting to excel from a custom class object using C#.NET

Get the Clicked Cell Value and Column Name of GridView in ASP.NET

How to hide bootStrap popover when user click(s) outside the popover?