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 someone help me figure out why this code is not working..

Status
Not open for further replies.

Wilnicm

MIS
Mar 16, 2009
20
US
I have this code when i run it after i add the values in the textbox its suppose to insert the values from the textbox into the database but its not doing it.. i don't know why its not here. I don't get no error message or anything..

System.Data.SqlClient.SqlConnection dataConnection = new SqlConnection();
dataConnection.ConnectionString =
@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyDiary.mdf;Integrated Security=True;User Instance=True";

System.Data.SqlClient.SqlCommand dataCommand = new SqlCommand();
dataCommand.Connection = dataConnection;

//tell the compiler and database that we're using parameters (thus the @first, @last, @nick)
dataCommand.CommandText = ("INSERT INTO UserProfile (Fname,Lname,UserName,Email,Pword,RePword) VALUES (@first,@last,@User,@Myemail,@MyPword,@MyRePword)");

//add our parameters to our command object
dataCommand.Parameters.AddWithValue("@first", first);
dataCommand.Parameters.AddWithValue("@last", last);
dataCommand.Parameters.AddWithValue("@User", User);
dataCommand.Parameters.AddWithValue("@MyPword", MyPword);
dataCommand.Parameters.AddWithValue("@Myemail", Myemail);
dataCommand.Parameters.AddWithValue("@MyRePword", MyRePword);


dataConnection.Open();
dataCommand.ExecuteNonQuery();
dataConnection.Close();

<Will The Great>
 
Have you put a debugger on those lines to see if they are hit when your button event (or whatever fires)?

Have you checked in the debugger to see if those variables being assigned are actually populated from the textboxes?

I also don't know if you should have that \ in the Attach Db Filename. A sample connection string from connectionstrings.net:

"Attach a database file, located in the data directory, on connect to a local SQL Server Express instance"

Code:
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;

That's all I have for now, hope it can be of some help.

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
should the parameters come before the SQL statement?
it doesn't matter. the command object, only builds the command. the sql is "put together" when you execute the command using the respective member.

one issue i see is the connection will only be closed, if an exception is not thrown. neither the command or connection is disposed of. both of these issues will cause side affects.

research try/finally blocks (no need to catch the exception, just let it bubble up), the IDisposable interface and the using key word.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top