Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can't inserted

Status
Not open for further replies.

jepe666

Programmer
Apr 17, 2008
36
ID
please help me,
i have coding like :
try
{
SqlCeConnection connectionstring = new SqlCeConnection(@"Data Source=" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\barang.sdf;Persist Security Info=False;"));
SqlCeDataAdapter da = new SqlCeDataAdapter("select * from barang", connectionstring);
DataSet ds = new DataSet();
da.Fill(ds, "barang");
MessageBox.Show("Connection successfull");
try
{
connectionstring.Open();
SqlCeCommand cmd = new SqlCeCommand("insert into barang(kdbarang) values(@kode)", connectionstring);
cmd.Parameters.Add("@kode", SqlDbType.NVarChar, 10);
cmd.Parameters["@kode"].Value = txtkode.Text;
cmd.ExecuteNonQuery();
MessageBox.Show("Records Inserted Successfully");
connectionstring.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

that code work well,no error...but data inserted not found in table..
can anyone help me??
sorry about my bad english...


thanks for advance
 
the code is correct. although I don't know why you are loading the dataset and then inserting new records.
1. they should happen in the opposite order if you want to see changes imedately.
2. this should be done in 2 different functions. (1 to get data, 2 to change data)

also your use of try/catch is a little off. 1. you never dispose of the connection/command. 2 you close the connection in the wrong place. currently if an exception is thrown the connection remains open. the accepted method for connection management is to simply dispose when you are done.
Code:
using(IDbConnection connection = new SqlConnection())
{
   connection.Open();
   using(IDbCommand command = connection.CreateCommand())
   {
         // preform CRUD.
   }
}
when displaying/logging an exception. Use ex.ToString() not ex.Message. ToString() contains the Message, Type and StackTrace. This is required to properly debug the error. The message alone is useless.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
thanks for response jmeckley,
thanks that's work...
very help me...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top