Adding a row in a database
-
Hi, I would like to add a record in the last row of a database from a c# program. I have a database with the first column called ID which is unique. I tried doing this by finding the maximum number in that column and then adding 1 to it but it doesn' work. Thanks
-
Hi, I would like to add a record in the last row of a database from a c# program. I have a database with the first column called ID which is unique. I tried doing this by finding the maximum number in that column and then adding 1 to it but it doesn' work. Thanks
-
Hi, I would like to add a record in the last row of a database from a c# program. I have a database with the first column called ID which is unique. I tried doing this by finding the maximum number in that column and then adding 1 to it but it doesn' work. Thanks
Set the id field to be a counter/identifier/auto_increment (depending on the flavour of database you are using). Then you just add a record without specifying the id at all, and it automatically gets a new unique id, without any risks of conflicts. --- b { font-weight: normal; }
-
Standard questions 1) What database? 2) "doesnt work" - what doesnt work and what error if any 3) show your code.
I have a database with some questions and each question has a unique ID. In my c# program I have a form with a datagrid showing all these questions and 2 textboxes, 1 showing the id of the question (txtQstID) and the other showing the question (txtQst). Then I have a button (btnAdd) that when I clicks it a new question is added in the datagrid. The new question is entered by the user in the txtQst textbox. But the ID of the question textbox is readonly so the user cannot enter anything in it. So I did the following in the add button: - //getting the maximum ID from the Qst_ID column System.Data.OleDb.OleDbCommand oCommandMax = new System.Data.OleDb.OleDbCommand("SELECT MAX(Qst_ID) FROM Questions", conn); object objMax = oCommandMax.ExecuteScalar(); string maxValue = objMax.ToString(); //parsing the maxValue to integer int newRecord = (int.Parse(maxValue)); //adding 1 to the maximum number newRecord++; //displaying the new ID in the txtQstID textbox this.txtQstID.Text = (newRecord.ToString()); //CreateNewQuestion is a method found in Class 1 Class1.CreateNewQuestion(txtQstID.Text, txtQst.Text); //The following is the code found in Class1: - public static void CreateNewQuestion(string ID, string Question) { DataRow drNew = DS.Tables["Questions"].NewRow(); drNew["Question_ID"] = ID; drNew["Question"] = Question; DS.Tables["Questions"].Rows.Add(drNew); }
-
I have a database with some questions and each question has a unique ID. In my c# program I have a form with a datagrid showing all these questions and 2 textboxes, 1 showing the id of the question (txtQstID) and the other showing the question (txtQst). Then I have a button (btnAdd) that when I clicks it a new question is added in the datagrid. The new question is entered by the user in the txtQst textbox. But the ID of the question textbox is readonly so the user cannot enter anything in it. So I did the following in the add button: - //getting the maximum ID from the Qst_ID column System.Data.OleDb.OleDbCommand oCommandMax = new System.Data.OleDb.OleDbCommand("SELECT MAX(Qst_ID) FROM Questions", conn); object objMax = oCommandMax.ExecuteScalar(); string maxValue = objMax.ToString(); //parsing the maxValue to integer int newRecord = (int.Parse(maxValue)); //adding 1 to the maximum number newRecord++; //displaying the new ID in the txtQstID textbox this.txtQstID.Text = (newRecord.ToString()); //CreateNewQuestion is a method found in Class 1 Class1.CreateNewQuestion(txtQstID.Text, txtQst.Text); //The following is the code found in Class1: - public static void CreateNewQuestion(string ID, string Question) { DataRow drNew = DS.Tables["Questions"].NewRow(); drNew["Question_ID"] = ID; drNew["Question"] = Question; DS.Tables["Questions"].Rows.Add(drNew); }
-
I have a database with some questions and each question has a unique ID. In my c# program I have a form with a datagrid showing all these questions and 2 textboxes, 1 showing the id of the question (txtQstID) and the other showing the question (txtQst). Then I have a button (btnAdd) that when I clicks it a new question is added in the datagrid. The new question is entered by the user in the txtQst textbox. But the ID of the question textbox is readonly so the user cannot enter anything in it. So I did the following in the add button: - //getting the maximum ID from the Qst_ID column System.Data.OleDb.OleDbCommand oCommandMax = new System.Data.OleDb.OleDbCommand("SELECT MAX(Qst_ID) FROM Questions", conn); object objMax = oCommandMax.ExecuteScalar(); string maxValue = objMax.ToString(); //parsing the maxValue to integer int newRecord = (int.Parse(maxValue)); //adding 1 to the maximum number newRecord++; //displaying the new ID in the txtQstID textbox this.txtQstID.Text = (newRecord.ToString()); //CreateNewQuestion is a method found in Class 1 Class1.CreateNewQuestion(txtQstID.Text, txtQst.Text); //The following is the code found in Class1: - public static void CreateNewQuestion(string ID, string Question) { DataRow drNew = DS.Tables["Questions"].NewRow(); drNew["Question_ID"] = ID; drNew["Question"] = Question; DS.Tables["Questions"].Rows.Add(drNew); }
-
. . .
System.Data.OleDb.OleDbCommand oCommandMax = new System.Data.OleDb.OleDbCommand("SELECT top1 * FROM Questions order by Question_ID desc", conn);
. . . //insetead newRecord++; increase by oneint newRecord = (int.Parse(maxValue)+1);
-
One of them will get an error message because they try to create a record with the same key. --- b { font-weight: normal; }