Problem while inserting data from GUI layer to database

Posted by Rahul on Stack Overflow See other posts from Stack Overflow or by Rahul
Published on 2010-02-28T03:57:24Z Indexed on 2011/03/06 16:10 UTC
Read the original article Hit count: 266

Filed under:
|

Hi all, I am facing problem while i am inserting new record from GUI part to database table. I have created database table Patient with id, name, age etc....id is identity primary key. My problem is while i am inserting duplicate name in table the control should go to else part, and display the message like...This name is already exits, pls try with another name... but in my coding not getting..... Here is all the code...pls somebody point me out whats wrong or how do this??? GUILayer:

protected void BtnSubmit_Click(object sender, EventArgs e)
{
        if (!Page.IsValid)
        return;
        int intResult = 0;
        string name = TxtName.Text.Trim();
        int age = Convert.ToInt32(TxtAge.Text);
        string gender;
        if (RadioButtonMale.Checked)
        {
            gender = RadioButtonMale.Text;
        }
        else
        {
            gender = RadioButtonFemale.Text;
        }
        string city = DropDownListCity.SelectedItem.Value;

        string typeofdisease = "";
        foreach (ListItem li in CheckBoxListDisease.Items)
        {
            if (li.Selected)
            {
                typeofdisease += li.Value;
            }
        }
        typeofdisease = typeofdisease.TrimEnd();
        PatientBAL PB = new PatientBAL();
        PatientProperty obj = new PatientProperty();
        obj.Name = name;
        obj.Age = age;
        obj.Gender = gender;
        obj.City = city;
        obj.TypeOFDisease = typeofdisease;  
        try
        {
            intResult = PB.ADDPatient(obj);
            if (intResult > 0)
            {
                lblMessage.Text = "New record inserted successfully.";
                TxtName.Text = string.Empty;
                TxtAge.Text = string.Empty;
                RadioButtonMale.Enabled = false;
                RadioButtonFemale.Enabled = false;
                DropDownListCity.SelectedIndex = 0;
                CheckBoxListDisease.SelectedIndex = 0;                     
            }
            else
            {
                lblMessage.Text = "Name [<b>" + TxtName.Text + "</b>] alredy exists, try another name";


            }                
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message.ToString();
        }
        finally
        {
            obj = null;
            PB = null;
        }        

}

BAL layer:

public class PatientBAL
{
public int ADDPatient(PatientProperty obj)
{
    PatientDAL pdl = new PatientDAL();
    try
    {
        return pdl.InsertData(obj);
    }
    catch
    {
        throw;
    }
    finally
    {
        pdl=null;
    }
}

}

DAL layer:

public class PatientDAL
{
public string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;    
public int InsertData(PatientProperty obj)
{
    SqlConnection con = new SqlConnection(ConString);
    con.Open();
    SqlCommand com = new SqlCommand("LoadData",con);
    com.CommandType = CommandType.StoredProcedure;

    try
    {
        com.Parameters.AddWithValue("@Name", obj.Name);
        com.Parameters.AddWithValue("@Age",obj.Age);
        com.Parameters.AddWithValue("@Gender",obj.Gender);
        com.Parameters.AddWithValue("@City", obj.City);
        com.Parameters.AddWithValue("@TypeOfDisease", obj.TypeOFDisease);
        return com.ExecuteNonQuery();
    }
    catch
    {
        throw;
    }
    finally
    {
        com.Dispose();
        con.Close();
    }          
}

}

Property Class:

public class PatientProperty
{
private string name;
private int age;
private string gender;
private string city;
private string typedisease;

public string Name
{
    get
    {
        return name;
    }
    set
    {
        name = value;
    }
}
public int Age
{
    get
    {
        return age;
    }
    set
    {
        age = value;
    }
}
public string Gender
{
    get
    {
        return gender;
    }
    set
    {
        gender = value;
    }
}
public string City
{
    get
    {
        return city;
    }
    set
    {
        city = value;
    }
}
public string TypeOFDisease
{
    get
    {
        return typedisease;
    }
    set
    {
        typedisease = value;
    }
}

}

This is my stored Procedure: CREATE PROCEDURE LoadData ( @Name varchar(50), @Age int, @Gender char(10), @City char(10), @TypeofDisease varchar(50) ) as insert into Patient(Name, Age, Gender, City, TypeOfDisease)values(@Name,@Age, @Gender, @City, @TypeofDisease) GO

© Stack Overflow or respective owner

Related posts about c#

Related posts about database