none
how to check whether there is already a record present in the Database with the same id as primary key

    Question

  • how to check whether there is already a record present in the Database with the same data as primary key,

    pls reply soooooon

    Saturday, September 25, 2010 10:44 AM

All replies

  • Hi,

    SqlCommand cmd=new SqlCommand("Select username,password from tablename where username='"+textuserName.Text+"' and password='"+txtpassword.Text+"'",conn);

     



    SqlDataReader dr=cmd.ExecuteReader();
    if(dr.Read())

    {
    MessageBox("userName is already availables");

    }
    else

    {

    //write here insert command
    }


    PS.Shakeer Hussain Hyderabad
    Saturday, September 25, 2010 11:27 AM
  • Thanks lot for ur reply, i want the codings in C#, im using ASP .Net C# ly. so kindly send the codings for this...
    Monday, September 27, 2010 5:13 AM
  • Hi Raji,

    I had send you the in C# only.Use the code in Button click event.

     

    SqlCommand cmd=new SqlCommand("Select username,password from tablename where username='"+textuserName.Text+"' and password='"+txtpassword.Text+"'",conn);

     

    SqlDataReader dr=cmd.ExecuteReader();
    if(dr.Read())
    {
    MessageBox("userName is already availables");
    }
    else
    {
    //write here insert command
    }

    PS.Shakeer Hussain Hyderabad
    Monday, September 27, 2010 5:22 AM
  • ya

    again error coming like "ExecuteReader: Connection property has not been initialized."

     

    im just want to check weather the record is already there in database,

    here im going to insert patient details, in dis Patient_Id is primary key, so i want to check weather the current id is already present i the database. thats all mu prob pls!!!!!!! Help me

     

     

     

     


    my code:

    using System;

    using System.Collections;

    using System.Configuration;

    using System.Data;

    using System.Linq;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.HtmlControls;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Xml.Linq;

    using System.Data.SqlClient;

     

    public partial class Default5 : System.Web.UI.Page

    {

     

        private void MessageBox(string msg)

        {

     

     

        }

     

     

     

     

     

     

        protected void Page_Load(object sender, EventArgs e)

        {

     

            SqlConnection conn = new SqlConnection("Data Source=UNIBROS-3;Initial Catalog=info;Integrated Security=True");

            conn.Open();

            SqlCommand cmd = new SqlCommand("Select Patient_Id from patient where Patient_Id ='" + TextBox1.Text + "'");

     

            SqlDataReader dr = cmd.ExecuteReader();

     

            if (dr.Read())

            {

     

                MessageBox("userName is already availables");

     

     

     

            }

     

            else

            {

     

                //write here insert command

     

            }

     

     

     

        }

     

        protected void TextBox1_TextChanged(object sender, EventArgs e)

        {

     

        }

    }


    Monday, September 27, 2010 6:28 AM
  • Hi,

    Pass the connection object in the SqlCommand as follows:

    you can check conn object written in Bold(conn)

    SqlConnection conn = new SqlConnection("Data Source=UNIBROS-3;Initial Catalog=info;Integrated Security=True");

     conn.Open();

    SqlCommand cmd = new SqlCommand("Select Patient_Id from patient where Patient_Id ='" + TextBox1.Text + "'", conn );

     

    and remaining code goes here.....


    PS.Shakeer Hussain Hyderabad
    Monday, September 27, 2010 7:14 AM
  • ya thank u very much but no error and also output also not comming

    i dont know weather its checking the record or not,

    datareader included in class file or wat?

    My code now:

    using System;

    using System.Collections;

    using System.Configuration;

    using System.Data;

    using System.Linq;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.HtmlControls;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Xml.Linq;

    using System.Data.SqlClient;

    using Ap = Application;

    public partial class Default5 : System.Web.UI.Page

    {


        

        protected Ap.Class1 oClass;

        protected void Page_Load(object sender, EventArgs e)

        {

            oClass = new Ap.Class1(Constant.ApplicationDB);

             

             if (!IsPostBack)

             {

             }

        }


           

     

         private void MessageBox(string msg){}


        protected void Button1_Click(object sender, EventArgs e)

         {

            

     

            SqlConnection conn = new SqlConnection("Data Source=UNIBROS-3;Initial Catalog=info;Integrated Security=True");

            conn.Open();

             SqlCommand cmd = new SqlCommand("Select Patient_Id from patient where Patient_Id ='" + TextBox1.Text + "'",conn);

     

             SqlDataReader dr = cmd.ExecuteReader();

     

     

             if (dr.Read())

             {

     

                 MessageBox("userName is already availables");

     

             }

     

             else

             {

     

                 //MessageBox("userName is already availables");

                 //write here insert command

     

             }

     

     

     

         }

    }

     

     

    Monday, September 27, 2010 7:39 AM
  • Hi,

    I think there is no record in a Table what you had inserted

    If record exist in a Table'if ' condition executes.

    if recored doesnt exists in a Table 'else ' condition executes.

    check the below condtion in your code..

     

     

    if (dr.Read())

     {

               MessageBox("userName is already availables");

     }

     else

     {

               MessageBox("userName is Not  available in a Table");      

      }


    PS.Shakeer Hussain Hyderabad
    Monday, September 27, 2010 7:46 AM
  • ya tried syed, but same,it didint display any msg box, i have 10 records in my database.
    Monday, September 27, 2010 8:29 AM
  • Hi,

    No.it should has to come..once put break point in button click event and check it by pressing 'F10' key...

    once send me your records what you have in your Table and What your entering in a Textbox..with your code

     


    PS.Shakeer Hussain Hyderabad
    Monday, September 27, 2010 8:33 AM
  •  

    Dis my record in database , i just entered POO1 for checking but it didin't show any error msg

     














    Patient_Id

    Patient_NameAgeGenderDateofbirthMaterialStatusBlood_GroupMobileNoContactNoAddressPatient_TypeRemarksDateofregistration p r 21 Female 11/7/1988 12:00:00 AM Married A+ve 0 04143255107 gfdiyt On Patient test 9/9/2010 5:34:17 PM P001 raji 22 Female 3/1/1988 12:00:00 AM Un Married O+ve 9778657646 04245546546 chennai On Patient cold

    9/27/2010 2:18:41 PM

     











     

     

    Monday, September 27, 2010 8:54 AM
  • Hi,

    Check the code once.I had send you a Proper code.Put Break Point and check it..

     


    PS.Shakeer Hussain Hyderabad
    Monday, September 27, 2010 8:58 AM
  • Hi,

    Once Run this SQL Query in your query analyzer.

    Select Patient_Id from patient where Patient_Id =' POO1'

     

    What you got the result tell me.


    PS.Shakeer Hussain Hyderabad
    Monday, September 27, 2010 9:01 AM
  • ya i checked in query analyzer it shows the answer., i gave P001 its shows P001 record,
    here my record in database


    p r 21 Female 1988-11-07 00:00:00.000 Married A+ve 0 04143255107 gfdiyt On Patient test 2010-09-09 17:34:17.000
    P001 raji 22 Female 1988-03-01 00:00:00.000 Un Married O+ve 9778657646 04245546546 chennai On Patient cold 2010-09-27 14:18:41.000
    Monday, September 27, 2010 9:22 AM
  • Hi,

    Once check your C# code proper...


    PS.Shakeer Hussain Hyderabad
    Monday, September 27, 2010 10:16 AM