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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting row into database

Status
Not open for further replies.

Jim318

Programmer
Jul 1, 2002
33
US
Hi,

I am new to ASP.net and I have designed a simple web form with a text box and dropdown box to add values into an access database table.

I am struggling to figure out how I can write those values as a new row into the table. Every reference book I look at deals with updating recordsets but I have not found anything on inserting new data.

Could anyone please provide me with a simple example of how I might be able to use a stored procedure or Insert query to add a new record to the table.

Thanks,
Jim
 
Sorry about the following mass of code. I just copied one of my insert methods. If you need more info or don't understand what it is that I did please feel free to ask.

public int AddTempUser(String FullName, String Email, String Password)
{
// Create Instance of Connection and Command Object
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand = new SqlCommand("AddTempUser", myConnection);
//Create Instance of Marks Components
MarksComponents.Encryptor myEncryptor = new MarksComponents.Encryptor(EncrytString, IV);

// Mark the Command as a SPROC
myCommand.CommandType = CommandType.StoredProcedure;

// Add Parameters to SPROC
SqlParameter parameterFullName = new SqlParameter("@Name", SqlDbType.VarChar, 50);
parameterFullName.Value = FullName;
myCommand.Parameters.Add(parameterFullName);

SqlParameter parameterEmail = new SqlParameter("@Email", SqlDbType.VarChar, 50);
parameterEmail.Value = Email;
myCommand.Parameters.Add(parameterEmail);

SqlParameter parameterPassword = new SqlParameter("@Password", SqlDbType.VarChar, 20);
parameterPassword.Value = myEncryptor.Encrypt(Password);//encrypt the password 1st
myCommand.Parameters.Add(parameterPassword);

SqlParameter parameterPayPalId = new SqlParameter("@PayPalID", SqlDbType.Int);
parameterPayPalId.Direction = ParameterDirection.Output;
myCommand.Parameters.Add(parameterPayPalId);

// Execute the command in a try/catch to catch duplicate username errors
try
{
// Open the connection and execute the Command
myConnection.Open();
myCommand.ExecuteNonQuery();
}
catch(Exception ex)
{
MarksComponents.Logger myLogger = new MarksComponents.Logger("", ConfigurationSettings.AppSettings["LogFile"]);

myLogger.ConnectionString = ConfigurationSettings.AppSettings["ErrorConnectionString"];

myLogger.AppID = Convert.ToInt32(ConfigurationSettings.AppSettings["AppID"]);
myLogger.Source = "DAL.Login";
//myLogger.SessionID = Session.SessionID;
myLogger.LogMessage(ex.ToString());

myLogger = null;

// failed to create a new user
return -1;
}
finally
{

// Close the Connection
if (myConnection.State == ConnectionState.Open)
myConnection.Close();
}

return (int) parameterPayPalId.Value;
}


That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Just in case you were asking at an even more detailed level, the correct sql syntax is

Insert Into
([Field1],[Field2]) Values ([Value1],[Value2])

D'Arcy
 
D'Arcy, Thanks that is all I needed to know!

Jim
 
doh!

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Thanks Mark I did appreciate your effort.

Jim
 
[bigcheeks]It's ok.
I just read the question wrong and found it humorus that D with his two line response got what you wanted when i posted a big huge speach

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top