C# -- DataGridView -- "ID" autogenerated

My current C# APP requires me to
 1. enter anything in the "ID" field (attached 1.jpg)
 2. click submit
 3. attached 2.jpg get updated in database with
    autogenerated "ID = 139266" based on below code

I then try to do the same thing without an "ID"
since it will automatically get created below,
but I get the attached 3.jpg

How can I fix ?
------------------------------------------------------------------
        private void btn_Submit_Click(object sender, EventArgs e)
        {
            this.dataSet1.EnforceConstraints = false;
            this.Validate();
            decimal iGML_ROMSTR_ID = 0;
            try
            {
                DataSet1TableAdapters.GML_SEQ_ROMSTRTableAdapter_ adpSeq = new MenuByUser.DataSet1TableAdapters.GML_SEQ_ROMSTRTableAdapter_();
                DataSet1.GML_SEQ_ROMSTRDataTable tb = adpSeq.GetData();

                if (tb != null && tb.Rows.Count > 0)
                {
                    if (tb.Rows[0][0] != DBNull.Value)
                    {
                        iGML_ROMSTR_ID = Convert.ToDecimal(tb.Rows[0][0]);
                    }
                }
                if (iGML_ROMSTR_ID <= 0)
                    iGML_ROMSTR_ID = 1;

                decimal TEMP_locNum = 0;
                decimal TEMP_palletsReq = 0;
                decimal TEMP_imId = 0;
                string TEMP_Priority = String.Empty;

                for (int i = 0; i < gML_ROITEMDataGridView.Rows.Count - 1; i++)
                {
                    TEMP_locNum = (decimal)this.gML_ROITEMDataGridView["dataGridViewTextBoxColumn2", i].Value;
                    TEMP_imId = (decimal)this.gML_ROITEMDataGridView["dataGridViewTextBoxColumn3", i].Value;
                    TEMP_palletsReq = (decimal)this.gML_ROITEMDataGridView["dataGridViewTextBoxColumn4", i].Value;
                    TEMP_Priority = this.gML_ROITEMDataGridView["dataGridViewTextBoxColumn5", i].Value.ToString();
                    //below inserts locnum-itemID-pallets-pri-date-user-RO#-vend#
                    this.gML_ROITEMTableAdapter.InsertQuery(TEMP_locNum, 177982, TEMP_palletsReq, TEMP_Priority, DateTime.Today, 42, iGML_ROMSTR_ID, Convert.ToDecimal(this.cboVendor.SelectedValue.ToString()));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Assign Error-> " + ex.Message);
            }
        }
------------------------------------------------------------------
1.jpg
2.jpg
3.jpg
finance_teacherAsked:
Who is Participating?
 
Craig WagnerSoftware ArchitectCommented:
If you look closely at the stack trace you provided, the problem is not in the code you posted. In fact, it's never even getting to your code, the entire call stack is showing framework code.

Where are the column definitions from the grid coming from? Are you creating a DataTable in code and binding it, or are you retrieving from the database and binding that? That is where the problem is, whatever you're using to define the columns in your grid says that the ID column doesn't allow null. You need to correct it at the source. Focusing on the method you posted will not get you to a solution because the exception is occurring before it ever hits your code.
0
 
rajeeshmcaCommented:
Hi finance_teacher,

Does your Id field in the Database allow null?
0
 
finance_teacherAuthor Commented:
ID is required, but the above ".InsertQuery" code does the
below "GML_SEQ_ROITEM.NEXTVAL" to automatically populate it.
-------------------------------------------
INSERT INTO GML_ROITEM
                      (ID, LOC_NUM, IM_ID, PALLETSREQ, PRIORITY, DATEORIG, USR_ID, ROM_ID, VENDNO)
VALUES     (GML_SEQ_ROITEM.NEXTVAL, :LOC_NUM, :IM_ID, :PALLETSREQ, :PRIORITY, :DATEORIG, :USR_ID, :ROM_ID, :VENDNO)
-------------------------------------------
Please help.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
rajeeshmcaCommented:
the insert is happening in the backend .. right??
0
 
finance_teacherAuthor Commented:
Yes
0
 
rajeeshmcaCommented:
i think the problem is that the tableadapter will expect an id... since this is enforced with the constraints of the table, it will definitely want the id...

Can you get the nexval before you click for the save... if so, then you can retrive the nextval()  in the cell leave event of the Id cell and assign it with what yoy get from the NextVal() function
0
 
ROMA CHAUHANProject LeadCommented:
Hi finance_teacher, If i m correct then u must have been using the bindingSource as datasource to the datagridView.
I mean u have assigning the datasource from the UI itself not from the code behind.

If you have done like this then do one thing select that dataset from UI right click on it. Go to edit dataset. Select that Id column and check the property of it.
The first property will be AllowDBNull=False make it True and Try.

It may solve your problem.
0
 
finance_teacherAuthor Commented:
Below steps worked
 1. open Form in DESIGN view
 2. click "dataSet1" COMPONENT
 3. Properties
 4. EnforceConstraints = False
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.