Column does not allow null

Please see my code below and tell me where I am going wrong.

case "particulars.aspx": dt = JobCardManager.GetParticularsByJobId(id); hidJobId.Value = id.ToString(); if (dt != null && dt.Rows.Count > 0) { gvParticulars.DataSource = dt; gvParticulars.DataBind(); } else { **dt.Rows.Add(dt.NewRow());** gvParticulars.DataSource = dt; gvParticulars.DataBind(); int TotalColumns = gvParticulars.Rows[0].Cells.Count; gvParticulars.Rows[0].Cells.Clear(); gvParticulars.Rows[0].Cells.Add(new TableCell()); gvParticulars.Rows[0].Cells[0].ColumnSpan = TotalColumns; gvParticulars.Rows[0].Cells[0].Text = "No Record Found"; } ddlJobs.Enabled = false; gvParticulars.ShowFooter = true; break; 

I want to ensure that in case dt has no lines, I need to display a new line in the footer, the controls are already added to the footer. My insert update and adding a new process are done from the grid itself.

So, if my datatable is null, then in the highlighted row I get the error: "Column does not allow null." Where in the database all columns have Allow Null flags. Please, help.

[Update]

I do not retrieve the primary key in the request. Editable parts of the table only.

+1
source share
2 answers

You can read the schema information when filling in the data set. I'm not sure if this will solve your problem, but I suspect it will be so. Without a schema, your data will not know if the column is null or not.

 da.FillSchema(ds, SchemaType.Source); da.Fill(ds); 
+3
source

Just guessing, but all columns cannot be null, but how are the columns (columns) defined as the primary key?

+1
source

Source: https://habr.com/ru/post/1343619/


All Articles