Column does not allow nulls

Please have a look at 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;

What I want to achieve is that in case when the dt don’t have any rows, I need to display a new row in the footer, controls are already added in footer. My insert update and add new process all are carried out from grid itself.

  • Is it possible in Dynamics CRM to run an SQL query like an advanced find and include selection boxes?
  • SQL Unpivot table
  • SQL sum by year report, looking for an elegant solution
  • INSERT Data From One Table Into Multiple Tables
  • Sort Bullets in Database
  • How do I get multiple rows when value > 1
  • So in case if my datatable comes null, then at the starred line, I am getting the error, “Column does not allow null”. Where in database all columns have Allow Null checkboxes are checked. Please help.

    [Update]

    I am not retriving primary key in the query. Only the editable parts of the table.

  • Dynamic pivot in SQL Server
  • SQL Server 2008 Error 233
  • Restore SQL Server DB without transaction log
  • Why is using OPENQUERY on a local server bad?
  • What does ON mean?
  • sqlalchemy MSSQL create database statement not allowed within multi-statement transaction
  • 2 Solutions collect form web for “Column does not allow nulls”

    You might want to read the schema information when you fill the dataset. I’m not sure whether this would solve your problem, but I suspect it will. Without the schema, your datatable won’t know whether the column accepts null or not.

    da.FillSchema(ds, SchemaType.Source);
    da.Fill(ds);
    

    Just a guess, but all columns cannot be null, what about the column(s) defined as the primary key?

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.