Overlaping of the record when Inserting the Record problem for the ID which is auto in Identity set to Yes incremented by 1 started 100 but program is the manual ID means user want to enter the ID manualy also if not then Auto ID must be show in IDtextox

Beantwortet Overlaping of the record when Inserting the Record problem for the ID which is auto in Identity set to Yes incremented by 1 started 100 but program is the manual ID means user want to enter the ID manualy also if not then Auto ID must be show in IDtextox

  • Freitag, 9. März 2012 22:49
     
     
    I try to Insert the record with images it it but problem is that i built the Database with Identity set to true means the ID is auto selected but my program in C# i built the program that it for manual ID mean user enter the ID which is not right because i set the Identity as true which is incremented with 1 started with 100 . Due to this reason i got the error i try to solve it but i cant . When i try to enter the record as
      

    Then the Error is like that and Yes i try to not enter the StudentID mean leave as blank then error is also occur which tell Column 'StudentID' is constrained to be unique.  Value '' is already present. how to solve this problem without touching the Identity which is incremented by one and started with 100 . Tell me please.  

    My AddModifyStudentRecords : Form Code is 

     DataSet StudentTrackerDataSet;
            DataRow currentRow;
           private DataSet StudentTrackerDataset;
           
            public AddModifyStudentRecords()
            {
                InitializeComponent();
            }
            // create two constructores One of these accept the Dataset as parameters and Other will be 
            // other will accept the DataSet and Datarow as the parameter
            public AddModifyStudentRecords(DataSet ds)
            {
                InitializeComponent();
                StudentTrackerDataSet = ds;
                currentRow = null;
            }
            public AddModifyStudentRecords(DataSet ds, DataRow row)
            {
                InitializeComponent();
                StudentTrackerDataSet = ds;
                textBox1.Text =currentRow["StudentID"] .ToString();
                textBox2.Text = currentRow["FirstName"].ToString();
                textBox4.Text = currentRow["LastName"].ToString();
                textBox3.Text = currentRow["Gender"].ToString();
                textBox5.Text = currentRow["GPA"].ToString();
                txtBrowseFile.Text = currentRow["MyImage"].ToString ();
                byte[] data = (byte[])currentRow ["MyImage"];
                MemoryStream ms = new MemoryStream(data);
                pictureBox1.Image = Image.FromStream(ms);
            }
            private void label3_Click(object sender, EventArgs e)
            {
            }
            private void button2_Click(object sender, EventArgs e)
            {
                string StudentID = textBox1.Text.ToString();
                string StudentFirstName = textBox2.Text.ToString();
                string StudentLastName = textBox4.Text.ToString();
                string Gender = textBox3.Text.ToString();
                string  GPA = textBox5.Text.ToString();
                Image  MyImage = pictureBox1.Image;
                DataTable table = StudentTrackerDataSet.Tables["Student"];
                if (currentRow == null)
                {
                    currentRow = table.NewRow();
                    currentRow["StudentID"] = textBox1.Text.ToString();
                    table.Rows.Add(currentRow );
                }
                currentRow .BeginEdit();
                currentRow ["StudentID" ]=StudentID ;
                currentRow["FirstName"] = StudentFirstName;
                currentRow["LastName"] = StudentLastName;
                currentRow["Gender"] = Gender;
                currentRow["GPA"] = GPA;
                currentRow["MyImage"] = convertToByte(txtBrowseFile.Text);        
                currentRow.EndEdit();
                Close();
            }
             byte[] convertToByte(string sourcePath)
            {   //get byte file size of image
                FileInfo FInfo = new FileInfo(sourcePath);
                long sizeByte = FInfo.Length;
                // read the File uisng file stream
                FileStream fs = new FileStream(sourcePath, FileMode.Open, FileAccess.Read);
                // read it agian as the byte using binary reader 
                BinaryReader br = new BinaryReader(fs);
                // convert image to byte already 
                byte[] data = br.ReadBytes((int)sizeByte);
                return data;
            }
            private void button1_Click(object sender, EventArgs e)
            {
                Close();
            }
            private void button3_Click(object sender, EventArgs e)
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    txtBrowseFile.Text = Path.GetFullPath(openFileDialog1.FileName);
                    pictureBox1.ImageLocation = txtBrowseFile.Text;
                }

Alle Antworten

  • Dienstag, 13. März 2012 09:08
    Moderator
     
     Beantwortet Enthält Code

    Hi muhammad,
    What’s the error message? 
    If the ID in database is Auto Increment, then the DataGridView will give a new value to the ID cell when you click on new row (I mean the last row of DataGridView).
    Please check whether the ID column of StudentTable is Auto Increment. If not, set Auto Increment to true.

    StudentTable.Columns["ID"].AutoIncrement = true;

    If you still have any doubt and concern about this issue, please let me know. If I misunderstood you, please kindly elaborate your question.

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us