Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

how to get auto increment value of field in sql server 2008 to integer variable in c# windows forms in another table.

$
0
0

hi my name is vishal.

Sorry to disturb you people.

But i have a problem.

I have form named:frmDoctor in which i  insert values into two tables named:doctor and doctordetail

Given below is structure of my 2 tables in sql server 2008.

table name: doctor

Column Name                                        DataType                            AllowNulls

doctor_id                                               Int(primary key,                       No

                                                             auto-increment field)

doctor_dob                                               date                                    Yes

doctor_sex                                            nvarchar(10)                          Yes

created_date                                        datetime                               Yes

user_id                                                  Int                                         Yes

table name: doctordetail

Column Name                                        DataType                            AllowNulls

doctor_first_name                                  nvarchar(50)                          Yes

doctor_middle_name                             nvarchar(50)                          Yes

doctor_last_name                                  nvarchar(50)                         Yes

row_upd_date                                       datetime                                Yes

agn                                                         Int(auto-increment                No

                                                                    field)

doctor_id                                                    Int                                     Yes

status                                                         bit                                      Yes

user_id                                                      Int                                      Yes

Given below is my c# code with sql server 2008 with which i insert data into my tables doctor and doctordetail in sql server 2008:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace DRRS_CSharp
{
    public partial class frmDoctor : Form
    {
        int doctorID;
        int pUserID;
        private string conString = "Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true";
        private DataSet dataset;
        private string SelectString = ("select p.doctor_id as doctor_id,p.doctor_dob as doctor_dob,n.doctor_first_name as doctor_fname, n.doctor_middle_name as doctor_mname,n.doctor_last_name as doctor_lname,p.doctor_sex as doctor_sex from doctordetail n,doctor p where n.doctor_id=p.doctor_id and n.status =1 and doctor_id=@doctorId");

        public frmDoctor()
        {
            InitializeComponent();
        }
 private void loadDoctor(string mDoctorID)
        {
            btnCreate.Text = "Save";
            SqlConnection conn = new SqlConnection(conString);
            SqlDataAdapter dap = new SqlDataAdapter(SelectString, conn);
            dap.SelectCommand.Parameters.Add("@doctorId", SqlDbType.VarChar, 50);
            dap.SelectCommand.Parameters["@doctorId"].Value = mDoctorID;
            dataset = new DataSet();
            dap.Fill(dataset, "doctor");
            dap.Fill(dataset, "doctordetail");
            txtFName.DataBindings.Add("Text", dataset, "doctordetail.doctor_fname");
            txtMName.DataBindings.Add("Text", dataset, "doctordetail.doctor_mname");
            txtLName.DataBindings.Add("Text",dataset,"doctordetail.doctor_lname");
            cboSex.DataBindings.Add("Text",dataset,"doctor.doctor_sex");
            dtDOB.DataBindings.Add("Text", dataset, "doctor.doctor_dob");
        }
private void btnCreate_Click(object sender, EventArgs e)
        {
            string dFieldName = "";
            Boolean vEmptyB = false;
            if (txtFName.Text.ToString().Trim() == "")
            {
                vEmptyB = true;
                dFieldName = "Please enter doctor's first name";
            }
            else if (cboSex.SelectedIndex == -1)
            {
                vEmptyB = true;
                dFieldName = "Please select gender of doctor";
            }
            if (vEmptyB == true)
            {
                MessageBox.Show(dFieldName + "should not be empty");
                return;
            }
            SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            DataTable dt = new DataTable();
            if (doctorID == 0)
            {
                cmd = new SqlCommand("Insert into doctor(doctor_sex,doctor_dob,created_date,user_id)" + "Values(@doctor_sex,@doctor_dob,GetDate(),@user_id)", conn);
                if (cboSex.SelectedIndex == 0)
                {
                    cmd.Parameters.AddWithValue("@doctor_sex", "M");
                }
                else
                {
                    cmd.Parameters.AddWithValue("@doctor_sex", "F");
                }
                cmd.Parameters.AddWithValue("@doctor_dob", dtDOB.Value);
                cmd.Parameters.AddWithValue("@user_id", pUserID);
                cmd.ExecuteNonQuery();
            }
            int vPatientID;
            if (doctorID == 0)
            {
                vPatientID = Convert.ToInt32(cmd.Parameters.AddWithValue("@doctor_id", SqlDbType.Int).Value);
            }
            else
            {
                cmd = new SqlCommand("Update doctordetail set status=0 where doctor_id=" + doctorID, conn);
                vPatientID = doctorID;
            }
            cmd = new SqlCommand("Insert into doctordetail(doctor_id,doctor_first_name,doctor_middle_name,doctor_last_name,row_upd_date,status,user_id)" + "Values(@doctor_id,@doctor_first_name,@doctor_middle_name,@doctor_last_name,GetDate(),@status,@user_id)", conn);
            cmd.Parameters.AddWithValue("@doctor_id", vPatientID);
            cmd.Parameters.AddWithValue("@doctor_first_name", txtFName.Text);
            cmd.Parameters.AddWithValue("@doctor_middle_name", txtMName.Text);
            cmd.Parameters.AddWithValue("@doctor_last_name", txtLName.Text);
            cmd.Parameters.AddWithValue("@status", 1);
            cmd.Parameters.AddWithValue("@user_id", pUserID);
            cmd.ExecuteNonQuery();
            ((MDIParent1)this.MdiParent).updateUserActivities(vPatientID, 2, txtFName.Text.ToString() + "doctor detail was added successfully");
            MessageBox.Show("doctor Detail was added successfully", "DRRS", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Close();

        }
 private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

Now in my doctor table in sql server 2008 i have a field name doctor_id which is my primary key and auto-increment field in table doctor.

Now what i want is value of doctor_id field in table doctor should be also transferred to doctor_id field in table named:doctordetail through c# code with sql server 2008.

But when i try the above code i get value of 8 only every time. I dont know why? Can anyone tell me how to transfer value of doctor_id field intable named:doctor to doctor_id field in table named:doctordetail by using c# code with sql server 2008.  What changes should i make in  my code above to get my required result? Can anyone help me please? Any help or guidance in solving this problem would be greatly appreciated! Can anyone help me


vishal


Viewing all articles
Browse latest Browse all 2535

Trending Articles