?
Solved

C# -- DataGridView -- "ID" autogenerated

Posted on 2009-12-21
8
Medium Priority
?
771 Views
Last Modified: 2013-12-17
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
0
Comment
Question by:finance_teacher
8 Comments
 
LVL 15

Expert Comment

by:rajeeshmca
ID: 26097430
Hi finance_teacher,

Does your Id field in the Database allow null?
0
 

Author Comment

by:finance_teacher
ID: 26097470
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
 
LVL 15

Expert Comment

by:rajeeshmca
ID: 26097544
the insert is happening in the backend .. right??
0
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
LVL 21

Accepted Solution

by:
Craig Wagner earned 668 total points
ID: 26097548
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
 

Author Comment

by:finance_teacher
ID: 26097549
Yes
0
 
LVL 15

Assisted Solution

by:rajeeshmca
rajeeshmca earned 668 total points
ID: 26097642
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
 
LVL 11

Assisted Solution

by:ROMA CHAUHAN
ROMA CHAUHAN earned 664 total points
ID: 26104014
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
 

Author Comment

by:finance_teacher
ID: 26104673
Below steps worked
 1. open Form in DESIGN view
 2. click "dataSet1" COMPONENT
 3. Properties
 4. EnforceConstraints = False
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question