0

I have a SQL Server Compact database function for insertion and selection .

public Exception SetPartyDetails(PartyDetails pd)
{
        query = "Insert into [Party Details] ( [Name],[Address],[Tin No],[Land Line No],[Mobile No]) values (@Name,@Address,@PartyTin,@LandLineNo,@MobileNo)";
        try
        {
            com = new SqlCeCommand(query , con);
            com.Parameters.AddWithValue("@Name",pd.name);
            com.Parameters.AddWithValue("@Address",pd.address);
            com.Parameters.AddWithValue("@PartyTin",pd.tinNo);
            com.Parameters.AddWithValue("@LandLineNo",pd.landLineNo);
            com.Parameters.AddWithValue("@MobileNo",pd.mobileNo);
            con.Open();
            com.ExecuteNonQuery();
            con.Close();
            retrn = null;

        }
        catch(Exception e)
        {
            con.Close();
            retrn = e;
        }
        return retrn;
}

public List<PartyDetails> GetAllPartyDetails()
{
        query = "select * from [Party Details] ";
        try
        {
            List<PartyDetails> pls = new List<PartyDetails>();
            con.Open();
            com = new SqlCeCommand(query, con);

            sdr = com.ExecuteReader();
            while (sdr.Read())
            {
                pd = new PartyDetails();
                pd.name = sdr.GetString(1);
                pd.address = sdr.GetString(2);
                pd.tinNo = sdr.GetString(3);
                pd.mobileNo = sdr.GetString(4);
                pd.landLineNo = sdr.GetString(5);
                pls.Add(pd);
            }
            con.Close();
            return pls;
        }
        catch (Exception)
        {
            con.Close();
            return null;
        }
}

When I am trying to call above function by following code

public static void Main()
{
    DataBase db = new DataBase();
    PartyDetails pd = new PartyDetails();
    pd.name = "Chet";
    pd.address = "b-77 ved villa colony ";
    pd.tinNo = "5346";
    pd.mobileNo = "88901505335725";
    pd.landLineNo = "014125353297610";

    Console.WriteLine(db.SetPartyDetails(pd) + " Sucess prd");

List<PartyDetails> pdl = db.GetAllPartyDetails();
Console.WriteLine(pdl.Count);
foreach (PartyDetails ps in pdl)
{
    Console.WriteLine(ps.name);
    Console.WriteLine(ps.address);
    Console.WriteLine(ps.landLineNo);
    Console.WriteLine(ps.mobileNo);
    Console.WriteLine(ps.tinNo);

}
}

When I run above code it runs as expected first insertion and then selection. Gives following output:

Output 
Sucess prd
1 
chet
b-77 ved villa colony
88901505335725
5346

But problem comes up when I run above code without calling insertion function only selection it gives output 0 which means it has no records but insertion was done before. I am not able to figure out what is wrong with the above code.

I tried few testing on the above code as follows

1.When i insert data from sql server management studio selection function works well .Suppose i insert 2 entries from management studio and one from above code. Selection function gives output of 3 entries which is right result but this only happens when i call insertion and selection simultaneously. With only selection call it gives two entries only which was inserted by Management studio.

2.When i insert 2-3 entries by insertion code and then call both function simultaneously it works well and give output of 2-3 entries.It means data is inserted in database because one time only entry is inserted but where the hell is it storing because in Management studio there is no data change.

3.Inserted data by code is stored temporarily because when i run my code after some time and call both function simultaneously it doesn't give entries inserted by code only gives entry by management studio ... as explained in point 1.

4

0 回答 0