Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,


When i am running a insert statement in the code it get executed finely. As all values are seen while debugging. I get the message that value is inserted successfully but when i check the database no value is seen in that. And when i again run the same code it gives me the message that this value is already present in the database. But i am not able to see the values in the database.

So can anyone tell me that why this happens?

The code is :
C#
try
{
  if (txtfname.Text == "" || txtlname.Text == "")
  {
    MessageBox.Show("Enter the first name and last name");
  }
  else
  {
    string ClientName = txtlname.Text + txtfname.Text + txtmname.Text;
    Address = txtadd.Text;
    City = txtcity.Text;
    LandlineNo = txtlandline.Text;
    MobNo = txtmobno.Text;
    EmailID = txtEmailID.Text;
    WebSite = txtwebsite.Text;
                        
    SqlCommand command = new SqlCommand("INSERT INTO Client(ClientName,Address,City,LandlineNo,MobNo,EmailID,WebSite)VALUES ('" + ClientName + "','"+Address+"','"+City+"','"+LandlineNo+"','"+MobNo+"','"+EmailID+"','"+WebSite+"')", con);
    con.Open();
    int i = command.ExecuteNonQuery();
    object CID;

    command.CommandText = "Select @@Identity";
    CID = command.ExecuteScalar();

    int ClientID = int.Parse(CID.ToString());

    con.Close();
    DialogResult result = MessageBox.Show("Client Info Added successfully", "Vehicle Info", MessageBoxButtons.OK);
    if (result == DialogResult.OK)
    {
      this.Close();
    }                 

  }
}
catch (Exception x)
{

  throw x;
}
Thanks in advance.
Posted
Updated 28-Jun-12 1:08am
v3
Comments
Kiran Susarla 28-Jun-12 6:27am    
How are you inserting the values? Can you please post your code here?
Yatin chauhan 28-Jun-12 6:27am    
Can u post some code which you are execute for insert query???

I'd like to address a few issues with your code.

First of all, this code is leaving database connections open if there is an exception. Classes like SqlCommand and SqlConnection implement IDisposable, so you can wrap them in a using statement which calls the Dispose method in all but the most terminal cases (in which case, an open database connection would be the least of your applications problems). So, your code would look like this:
C#
using (SqlConnection con = new SqlConnection(...){ ... }
Second issue, don't catch an exception if you don't do anything with it. Your try/catch block is useless because you merely rethrow your exception.

Third, your code is wide open to a Sql Injection attack. Use SQL parameters instead of explicit text injection.

Fourth, don't use SELECT @@IDENTITY. If you were to add a trigger to this table which inserted into another table, you would get the identityf from THAT table instead. Use SELECT SCOPE_IDENTITY() instead.
 
Share this answer
 
v3
By any chance, are you using a SQL Server file (i.e. SQL Server Compact)? If so, the database you think you're looking at might not be the database you are looking at.

OK, so what do I mean by that? Well, if you look at your database file in your project directory, it doesn't contain the values that you have inserted, but that's not the database your application sees. Instead, when you compile your application, one stage looks to see what files need copying to the output directory (bin\debug or bin\release), and I'm going to bet that you have that file marked as Copy if newer. This means that you actually have 2 databases - the one in the project folder, and the one in the output folder. The application sees this one. So, to see your data, all you need to do is open that one up and have a look at the data inside it.

I hope this helps.
 
Share this answer
 
whats your query?? how should we know where the error is coming??
post your query to help you.
Check your database fields, datatype and length.
 
Share this answer
 
Hi ,
Check your connection string maybe you are using old connection or maybe it refer to something else ,My advice for you use STORED PROCEDURE and always select SCOPE_IDENTITY() , @@IDENTITY to know if it success or not and start use using blocks
Best Regards
M.Mitwalli
 
Share this answer
 
public string wsAddContactList(string ContactName, string ContactEmailID, string ContactType, string OrganizerID)
{
string result = string.Empty;
try
{
object Contactid;

Object exist = contactlistview.checkAttendeeContactEmailID(ContactEmailID);

if (exist == null)
{
SqlCommand cmd = new SqlCommand("INSERT INTO Organizer_ContactList(ContactName,OrganizerID,ContactType) VALUES ('" + ContactName + "','" + OrganizerID + "', '" + ContactType + "')", con);
con.Open();
int i = cmd.ExecuteNonQuery();

cmd.CommandText = "Select @@Identity";
Contactid = cmd.ExecuteScalar();

int ContactID = int.Parse(Contactid.ToString());

SqlCommand cmd1 = new SqlCommand("INSERT INTO Contact_Email(ContactID, ContactEmailID) VALUES ('" + ContactID + "','" + ContactEmailID + "')", con);
int j = cmd1.ExecuteNonQuery();
con.Close();

if (j > 0)
{
result = ContactID.ToString();
}
else
{
result = "-1";
}
}
}
catch (Exception ex)
{
ExceptionError er = new ExceptionError();
er.errorMessage(ex.Message);
}
return result;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900