Click here to Skip to main content
15,911,711 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Database Design as follows

Column Data Type
ID Text (Primary Key)
Faculty Code text
course Code text

design as follows;

Id Combobox
Faculty Code Combo box
Course Code Combo box

New Save Clear (Buttons)


New button code as follows;

C#
private void Btn_New_Click(object sender, EventArgs e)
        {
            GFun.BindAccessConn();
            OleDbCommand cmd = new OleDbCommand("select MAX(ID) +1 from Tb_Faculty_Approval", GFun.OleDbCon);
            OleDbDataReader oledr = cmd.ExecuteReader();
            while (oledr.Read())
            {
                cb_id.Text = oledr[0].ToString().Trim();
            }

        }


The ID is the auto increment for that i written a code in the new button.

In database i set ID as Primary key.

In the run mode i click the new button the ID 1 will appear.
In faculty Code select the faculty
In the Course Code Select the couse

Then when i click the Save Button the error shows as follows;


Index or Primary Key cannot contain  a Null Value.

what is the problem in the New Button code.

Or in the database design in the ID column want to change?

please help me.

Thanks & Regards,
Narasiman P.
Posted
Updated 19-Feb-13 19:51pm
v3
Comments
Rockstar_ 20-Feb-13 1:54am    
To work this code, at least one record should be there in database.

The better way to auto increment ise set identity to the column.
use this query to alter your column
alter table table1 add col3 int identity(1,1)


Now you do not need to insert this value.Each time new row inserted,ID column incremented by 1.

Try it.
 
Share this answer
 
You can use it...
C#
public double maxid(string tablename)
      {
          double number = 0; ;
          SqlDataAdapter da = new SqlDataAdapter("select max(id) from " + tablename + " ", con);
          SqlDataAdapter da2 = new SqlDataAdapter("select * from " + tablename + " ", con);
          DataSet ds = new DataSet();
          DataSet ds2 = new DataSet();
          da.Fill(ds, tablename);
          da2.Fill(ds2, tablename);
          double num = ds2.Tables[0].Rows.Count;
          if (num.ToString() == "0")
          {
              number = 0;
          }
          else
          {
              number = Convert.ToDouble(ds.Tables[0].Rows[0][0]);
          }
          return (number);
      }


or you can check if max(id) is dbnull then return 0....with only select max(id) from tablename

thanks
Asp.Net C# Help Blog[^]
 
Share this answer
 
Comments
[no name] 20-Feb-13 2:15am    
it is msaccess dtabase

manually in the database i add new record and save it.

in the run mode, when i lcick the new button in the ID column 2 appear
select the faculy
select the course

then i click the save button error shows as follows
index or primary key cannot contain null values.


it is ms access database


please help me.
Hemant Singh Rautela 20-Feb-13 2:16am    
can you show your table design, & you can change above code for acess also

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900