I have been trying to add Datarow to empty Datatable that has one not null column through C#. Finally this DataTable will be used as DataContext for my UI.
Code Snippet:
string sqlStmt = "select * from Vendor where 1 = 0"; //sql to fetch empty datatable daVendor = new SqlDataAdapter(sqlStmt, sqlConnect); daVendor.MissingSchemaAction = MissingSchemaAction.AddWithKey; SqlCommandBuilder cmdbldr = new SqlCommandBuilder(daVendor); daVendor.Fill(dsVendor,"Vendor"); DataRow dataRow = dsVendor.Tables["Vendor"].NewRow(); dataRow["VendorID"] = Convert.ToInt32(VendorUtils.BPMUtilitiesCore.GetScalar("select IDENT_Current('Vendors.Vendor')")) + 1; dsVendor.Tables["Vendor"].Rows.Add(dataRow); //getting error here this.DataContext = dsVendor.Tables["Vendor"];
I am getting error that Column 'Company' does not allow nulls while trying to add dataRow to Datatable. I know that Column 'Company' in NOT NULL in DB.
But I need to add an empty row in Datatable in order to perform insertion through it thereafter. Also I don't want to assign any default value or empty string to
dataRow["Company"]
before adding this row to Datatable, as I have my custom Datarow validation for Not Null Columns in UI.
So, Is there any workaround to fix this issue with above approach? pls let me know at the earliest.
Thanks in advance.