Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
Hi ,

Lately i was trying to develop a windows application with c# n sql 2005. i got stucked at a point where i can insert data into the sql db but when i close n open my application, the data is gone. i'll explain briefly,
i've created the sql database(i.e, SKTDB.mdf) and table using server explorer in visual studio 08. my connection string is like ,
C#
sDBPath = Application.StartupPath + "\\DB\\SKTDB.mdf";
 if (System.IO.File.Exists(sDBPath))
 {
   connStr = "Data Source=.\\SQLEXPRESS;AttachDbFilename='" + sDBPath + "';User ID=sa;Password=1234";
 }


I have a registration form in my app. when i fill the form and submit , the data is inserted into the table in SKTDB.mdf. but when i restarts my application , the table is empty. I cant figure out the reason. if u guys can , pls let me know..
Posted
Comments
thatraja 16-Feb-12 5:48am    
Show the code(Data insert/update)
arun.emm 16-Feb-12 7:49am    
Here's my coding..

SqlTransaction OraTrans = null;
string sNoOfRecAff = null;
string sDBPath = Application.StartupPath + "\\DB\\SKTDB.mdf";
if (System.IO.File.Exists(sDBPath))
{
strConnString = "Data Source=.\\SQLEXPRESS;AttachDbFilename='" + sDBPath + "';User ID=sa;Password=1234";
}
string strSQL = "INSERT INTO EmpDetails(StaffCode, FirstName, LastName, Gender, DOB, Age, Father_Hus_Name, Address, Mobileno, Experience, prevCompAdd, DOJ, Designation, BasicSalary) VALUES(" + txt_code.Text + ",'" + txt_fname.Text + "','" + txt_lname.Text + "','" + g + "','" + dob + "','" + txt_age.Text + "','" + txt_father.Text + "','" + txt_addr.Text + "','" + txt_mobile.Text + "','" + cb_exp.SelectedItem.ToString() + "','" + txt_previousco.Text + "','" + doj + "','" + txt_desig.Text + "','" + txt_basic.Text + "')";
try
{
myConn = new SqlConnection(strConnString);
myConn.Open();
OraTrans = myConn.BeginTransaction();
//myCmd.Transaction = OraTrans;
myCmd = new SqlCommand(strSQL, myConn, OraTrans);
sNoOfRecAff = myCmd.ExecuteNonQuery().ToString();
OraTrans.Commit();
OraTrans.Dispose();
return sNoOfRecAff;
}

catch (Exception ex)
{
OraTrans.Rollback();
return ex.Message;
}

finally
{
myConn.Close();
myCmd.Dispose();
myConn.Dispose();
}

1 solution

Hi Arun ,

This is not a process of database programming with C#.
I will explain you briefly

Suppose you have below information to be save in database.
1) Full Name of user
2) Mobile number of user
3) Location of user

Then Create this table into sql server
SQL
Create table registration
(
 regid uniqueidentifier, fullname nvarchar(255),Mobile nvarchar(50),Location nvarchar(50)
)

then create this procedure
SQL
CREATE PROCEDURE Saveregistration 
(
 @Full NVARCHAR(50),
 @Mobile NVARCHAR(50),
 @Location NVARCHAR(50)
)
AS
BEGIN
  INSERT INTO registration
  select newid(), @Full,@Mobile,@Location
END


then use below code to call this procedure
C#
 SqlConnection con ;
 con=new SqlConnection("Data Source=;initial catalog=Northwind;User Id=;Password= '");
con.open();

SqlCommand command = new SqlCommand("Saveregistration ",con);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@Full",SqlDbType.NVARCHAR,50,txtfullname.text));
command.Parameters.Add(new SqlParameter("@Mobile ",SqlDbType.NVARCHAR,50,txtMobile.txt));
command.Parameters.Add(new SqlParameter("@Location ",SqlDbType.NVARCHAR,50,txtLocation.txt));

  int i=command.ExecuteNonQuery();



Hope this helps if yes then accept and vote the answer otherwise revert back with your queries
--Rahul D.
 
Share this answer
 
Comments
arun.emm 16-Feb-12 7:29am    
Hi Rahul ,
Thanks for your reply.. But i dont want to create database and tables in sql management studio. I want to create database within the application folder using server explorer of vs 2008. And i've created it already but got a problem which i have stated above.
RDBurmon 16-Feb-12 7:31am    
I suggest you instead of this
SqlConnection con ;
con=new SqlConnection("Data Source=;initial catalog=Northwind;User Id=;Password= '");
con.open();

sDBPath = Application.StartupPath + "\\DB\\SKTDB.mdf";
if (System.IO.File.Exists(sDBPath))
{
connStr = "Data Source=.\\SQLEXPRESS;AttachDbFilename='" + sDBPath + "';User ID=sa;Password=1234";
}

Which is perfectly fine
Hope this helps if yes then accept and vote the answer otherwise revert back with your queries
--Rahul D.


in my example
use your way of connection string
arun.emm 16-Feb-12 7:54am    
Here's my coding..
SqlTransaction OraTrans = null;
string sNoOfRecAff = null;
string sDBPath = Application.StartupPath + "\\DB\\SKTDB.mdf";
if (System.IO.File.Exists(sDBPath))
{
strConnString = "Data Source=.\\SQLEXPRESS;AttachDbFilename='" + sDBPath + "';User ID=sa;Password=1234";
}
string strSQL = "INSERT INTO EmpDetails(StaffCode, FirstName, LastName, Gender, DOB, Age, Father_Hus_Name, Address, Mobileno, Experience, prevCompAdd, DOJ, Designation, BasicSalary) VALUES(" + txt_code.Text + ",'" + txt_fname.Text + "','" + txt_lname.Text + "','" + g + "','" + dob + "','" + txt_age.Text + "','" + txt_father.Text + "','" + txt_addr.Text + "','" + txt_mobile.Text + "','" + cb_exp.SelectedItem.ToString() + "','" + txt_previousco.Text + "','" + doj + "','" + txt_desig.Text + "','" + txt_basic.Text + "')";

try
{
myConn = new SqlConnection(strConnString); myConn.Open(); OraTrans = myConn.BeginTransaction();
//myCmd.Transaction = OraTrans;
myCmd = new SqlCommand(strSQL, myConn, OraTrans);
sNoOfRecAff = myCmd.ExecuteNonQuery().ToString();
OraTrans.Commit();
OraTrans.Dispose();
return sNoOfRecAff;
}
catch (Exception ex)
{
OraTrans.Rollback();
return ex.Message;
}
finally
{
myConn.Close();
myCmd.Dispose();
myConn.Dispose();
}

is this a right way to do it?..
RDBurmon 16-Feb-12 8:05am    
Try this as well

myConn.Commit();

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