Monday, March 26, 2012

problem with SQL and c#

i have a very weird thing going on in my application program..

you see..

my c# application program has an sql connection..

and it can save records..

using stored procedures

but as soon as i stop debugging it

and then re-run it again..

all the records saved in my Database

literally disappears.!!

i need help..

please help out a young kid here...T_T

im very confused now..

*sigh*

Is the insert within a transaction that you are possibly not completing during your debugging session?

You also have to be careful when debugging C# or any .NET program. The line of code that you are on when you stop debugging is not necessarily the last line of code that executes. .NET programs complete the current thread that they are running before exiting. This means that although you think you have not hit a certain line of code that you actually have.

|||

It is possible that something is going wrong with your transaction, and so it is being rolled back when you stop debugging. There is a neat utility called "Sql Profiler" that we package free with SQL server that will enable you to see what is going on to debug this problem.

I hope this helps,

John (MSFT)

|||

can you please kindly discuss to me

this SQL profiler..

to tell you the truth..

i'm just a newbie in doing this stuff

you see..

my stored procedures follows

the program flow..

after executing that..

uhm..i close the sql connection..

is that right?

after that..

i have this line of code..

here's my actual sample program..

try

{

SqlConnection connect = new SqlConnection();

connect.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integrated Security=True;User Instance=True";

connect.Open();

SqlCommand cmd = new SqlCommand();

cmd.CommandText = "AddMembers";

cmd.CommandType = CommandType.StoredProcedure;

cmd.Connection = connect;

cmd.Parameters.Clear();

cmd.Parameters.Add("@.LastName", SqlDbType.VarChar, 50);

cmd.Parameters["@.lastName"].Value = txtLastName.Text;

cmd.Parameters.Add("@.FirstName", SqlDbType.VarChar, 50);

cmd.Parameters["@.FirstName"].Value = txtFirstName.Text;

cmd.Parameters.Add("@.Address", SqlDbType.VarChar, 50);

cmd.Parameters["@.Address"].Value = txtAddress.Text;

cmd.Parameters.Add("@.MobileNo", SqlDbType.VarChar, 50);

cmd.Parameters["@.MobileNo"].Value = txtMobileNo.Text;

cmd.Parameters.Add("@.EmailAdd", SqlDbType.VarChar, 50);

cmd.Parameters["@.EmailAdd"].Value = txtEmailAdd.Text;

cmd.ExecuteNonQuery();

MessageBox.Show("Membership successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

connect.Close();

this.membersTableAdapter1.Fill(this.database1DataSet2.Members);

txtLastName.Text = string.Empty;

txtFirstName.Text = string.Empty;

txtAddress.Text = string.Empty;

txtMobileNo.Text = string.Empty;

txtEmailAdd.Text = string.Empty;

txtLastName.ReadOnly = true;

txtFirstName.ReadOnly = true;

txtAddress.ReadOnly = true;

txtMobileNo.ReadOnly = true;

txtEmailAdd.ReadOnly = true;

btnAdd.Text = "&Add";

}

catch (Exception ex)

{

MessageBox.Show(ex.GetBaseException().ToString(), "Connection Status");

}

plase tell me if there's sumthin wrong bout my code..

as i've said earlier im just beginning to

understand the ropes of c# connected with SQL

thanks a whole lot to the help!!!

|||

to tell you the truth..

i'm just a newbie in doing this stuff

you see..

my stored procedures follows

the program flow..

after executing that..

uhm..i close the sql connection..

is that right?

after that..

i have this line of code..

here's my actual sample program..

try

{

SqlConnection connect = new SqlConnection();

connect.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integrated Security=True;User Instance=True";

connect.Open();

SqlCommand cmd = new SqlCommand();

cmd.CommandText = "AddMembers";

cmd.CommandType = CommandType.StoredProcedure;

cmd.Connection = connect;

cmd.Parameters.Clear();

cmd.Parameters.Add("@.LastName", SqlDbType.VarChar, 50);

cmd.Parameters["@.lastName"].Value = txtLastName.Text;

cmd.Parameters.Add("@.FirstName", SqlDbType.VarChar, 50);

cmd.Parameters["@.FirstName"].Value = txtFirstName.Text;

cmd.Parameters.Add("@.Address", SqlDbType.VarChar, 50);

cmd.Parameters["@.Address"].Value = txtAddress.Text;

cmd.Parameters.Add("@.MobileNo", SqlDbType.VarChar, 50);

cmd.Parameters["@.MobileNo"].Value = txtMobileNo.Text;

cmd.Parameters.Add("@.EmailAdd", SqlDbType.VarChar, 50);

cmd.Parameters["@.EmailAdd"].Value = txtEmailAdd.Text;

cmd.ExecuteNonQuery();

MessageBox.Show("Membership successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

connect.Close();

this.membersTableAdapter1.Fill(this.database1DataSet2.Members);

txtLastName.Text = string.Empty;

txtFirstName.Text = string.Empty;

txtAddress.Text = string.Empty;

txtMobileNo.Text = string.Empty;

txtEmailAdd.Text = string.Empty;

txtLastName.ReadOnly = true;

txtFirstName.ReadOnly = true;

txtAddress.ReadOnly = true;

txtMobileNo.ReadOnly = true;

txtEmailAdd.ReadOnly = true;

btnAdd.Text = "&Add";

}

catch (Exception ex)

{

MessageBox.Show(ex.GetBaseException().ToString(), "Connection Status");

}

plase tell me if there's sumthin wrong bout my code..

as i've said earlier im just beginning to

understand the ropes of c# connected with SQL

thanks a whole lot to the help!!!

|||See this blog here, that is purely a development setting problem as you are using a user instance.

http://blogs.msdn.com/sqlexpress/archive/2006/07/17/668971.aspx

Jens K. Suessmeyer.

http://www.sqlserver2005.de
|||

thanks a lot for helping me out..

that weblink just made it for me..

i havent tried yet using a deployment class as of yet..

since the copy to directory property modifications worked out

just as well for me..

at least for the time being..^_^

jeez..i owe it to you jens..

you sure know this huh?!

well..

my sincerest thanks to all the help i've got here..

No comments:

Post a Comment