0

Okay so this is a bit of a double question problem, but they are both related. The first question is how would I set a default value for my column as I want it to be true/1 (it's a bit column in case you hadn't guessed) and the second issue is that I have a row of checkboxes and I want these to be ticked when I have the value set as true, my current command is:

cmd.CommandText = "ALTER TABLE tbl_ecom_cat_feature ADD display_on_search BIT";
nemesv
  • 138,284
  • 16
  • 416
  • 359
  • 1
    Your first question answered here http://stackoverflow.com/questions/92082/add-column-with-default-value-to-existing-table-in-sql-server, the second could be possible if you are using bool fields for the bind – V4Vendetta May 21 '12 at 11:00
  • Thank you very much, I don't know how to give a great comment reply but thank you! – Norton Taylor May 21 '12 at 11:05

2 Answers2

1

Supposing you already have created the column, you could send this command

cmd.CommandText = "ALTER TABLE tbl_ecom_cat_feature ADD CONSTRAINT [DF_tbl_ecom_cat_feature_display_on_search]  DEFAULT (1) FOR [display_on_search]

To answer your second question I need to know where you display your data.
If it is a DataGridView then I think you should set the corresponding column to DataGridViewCheckBoxColumn as explained here

Steve
  • 213,761
  • 22
  • 232
  • 286
  • Well, the programs UI is using WPF and the data is displayed in a column called "Display On Search" that has a row of checkboxes that currently show up based on if the "Name" column isn't null – Norton Taylor May 21 '12 at 11:07
  • In a more informative responce, here is how I am doing it in my XML file – Norton Taylor May 21 '12 at 11:09
  • Not an expert on WPF, but I see that there is the same pattern there. DataGrid and [DataGridCheckBox](http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridcheckboxcolumn.aspx) – Steve May 21 '12 at 11:11
  • http://pastebin.com/BQkcxupU - This includes the xmal.cs method that is used to show it. – Norton Taylor May 21 '12 at 11:12
1

For Get Default Value Use : SELECT * FROM INFORMATION_SCHEMA.COLUMNS Where ...

For Set Default Value Use : ALTER TABLE {TABLENAME} ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL} CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}

mehdi lotfi
  • 11,194
  • 18
  • 82
  • 128