Newbie question: Adding records without data binding in c#?

D

DC

Is there an wasy way to ADD a record to and access database using
asp.net without getting involved in databinding my form to the database?

Esentially I want the form to be edited by a user and on the button
click subroutine will verify the fields add the record to the database.

Any eample code available out there, all the .net examples in c# seem to
be for databound controlls?
--
_______________________________________________

DC

"You can not reason a man out of a position he did not reach through reason"

"Don't use a big word where a diminutive one will suffice."

"A man with a watch knows what time it is. A man with two watches is
never sure." Segal's Law
 
T

Tim_Mac

hi DC,
here is some c# code which you can just insert into a web page. make
sure to include the System.Data.OleDb namespace. it uses the
ExecuteNonQuery method. it's a pure code example and doesn't use any
controls or databinding with user interfaces.

string sql = "insert into myTable(Col1,Col2) values('hello','fred')";
OleDbConnection Conn = new OleDbConnection(connString);
OleDbCommand cmd = new OleDbCommand(sql,Conn);
try
{
cmd.Connection.Open();
int numRecordsAffected = cmd.ExecuteNonQuery());
if(numRecordsAffected == 0)
// if this is a problem then inform the user
else
// tell the user the operation was successful
}
catch(Exception ex)
{
// do something
}
finally
{
cmd.Connection.Close();
}


hope this helps
tim
..Net blog: http://tim.mackey.ie
 
D

DC

Thank you very useful.

Just another quick question, Im now getting

" Compiler Error Message: CS1502: The best overloaded method match for
'System.Data.OleDb.OleDbCommand.OleDbCommand(string,
System.Data.OleDb.OleDbConnection)' has some invalid arguments"

When I run the following code

string strConnectionString, strQueryString;
strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data
Source='C:\\Program Files\\Common Files\\ODBC\\Data
Sources\\seminars.mdb';";

DateTime dtAddSeminarDate = new DateTime();
dtAddSeminarDate = DateTime.Parse(tbSeminarDate.Text);

string AddSemSql = "INSERT INTO SeminarList (Speaker, SpeakerEmail,
SpeakerInstitution,"
+ "SeminarTitle, SeminarDate, SeminarTime, SeminarLocation,
SeminarDetails, SeminarHost,"
+ " SeminarHostEmail, SeminarType, OrganisationNotes)"
+ " VALUES (" + tbSpeaker.Text + "," + tbSpeakerEmail.Text + "," +
tbSpeakerInstitution.Text +","
+ tbSeminarTitle.Text + "," + dtAddSeminarDate + "," +
tbSeminarTime.Text + "," + tbSeminarLocation.Text + ","
+ tbSeminarDetails.Text + "," + tbSeminarHost.Text + "," +
tbSeminarHostEmail.Text + "," + tbSeminarType.Text + "," +
tbOrganisationNotes.Text +")";

OleDbCommand cmd = new OleDbCommand(AddSemSql, strConnectionString);

Any ideas?

--
_______________________________________________

DC

"You can not reason a man out of a position he did not reach through reason"

"Don't use a big word where a diminutive one will suffice."

"A man with a watch knows what time it is. A man with two watches is
never sure." Segal's Law
 
T

Tim_Mac

hi DC,
with time you will learn that the compiler error messages are your best
clue to what is going wrong!!
in this case, it is a very useful message. 'invalid arguments' usually
means you are sending in the wrong type of objects/variables to a
method.
if you are using visual studio, when you open the bracket for the list
of arguments for a method, the type and name of the arguments appears
as a tooltip. another way to get this to display is to press
Shift+Ctrl+Space inside the brackets. if you don't have VS you can
look in the .net documentation that came with the SDK. do a search for
the OleDbCommand object, and look at the constructors.

you will see that it needs a string (which you have given it), and an
OleDbConnection object, which you nearly-but-not-quite gave it. you
passsed in a connection string, which is not the same as a connection
object! the compiler error also tells you the type of parameters the
method was expecting, so you don't really need to look up the
documentation in this case. it is so useful to get familiar with the
docs because it is your fastest learning resource.

you need to do something like:
OleDbConnection conn = new OleDbConnection(strConnectionString);
OleDbCommand cmd = new OleDbCommand(AddSemSql, conn);
conn.Open()....

hope this helps
tim
 
D

DC

Cheers, im now gettting the following exception with this code though...

INSERT INTO SeminarList (Speaker, SpeakerEmail,
SpeakerInstitution,SeminarTitle, SeminarDate, SeminarTime,
SeminarLocation, SeminarDetails, SeminarHost, SeminarHostEmail,
SeminarType, OrganisationNotes) VALUES ('Speaker
Name','(e-mail address removed)', 'Our Dept','Seminar Title',#25/05/2005
00:00:00#,'12:00','Main Theatre (GU01)','Add some Seminar
Details','Seminar organiser','(e-mail address removed)','Lunchtime
Seminar','Organisational Notes')

Provider=Microsoft.Jet.OLEDB.4.0; Data Source='C:\Program Files\Common
Files\ODBC\Data Sources\seminars.mdb';

There was an exception errorSystem.Data.OleDb.OleDbException: Operation
must use an updateable query. at
System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr)
at
System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS
dbParams, Object& executeResult) at
System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior
behavior, Object& executeResult) at
System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior
behavior, String method) at
System.Data.OleDb.OleDbCommand.ExecuteNonQuery() at
ASP.addmeeting_aspx.btnAddSeminar_Click(Object sender, EventArgs e) in
C:\inetpub\wwwroot\scripts\meetingsdisplay\addmeeting.aspx:line 54

Is this a secrity issue witht he database or is it something else?
--
_______________________________________________

DC

"You can not reason a man out of a position he did not reach through reason"

"Don't use a big word where a diminutive one will suffice."

"A man with a watch knows what time it is. A man with two watches is
never sure." Segal's Law
 
T

Tim_Mac

hi,
see the following site: http://support.microsoft.com/kb/q175168/

as an aside note: it is customary to do at least some research on a
problem before you post to the newsgroup. this is a very common error
and there's lots about it online. a google-groups search for this error
brings up 23000 results :)
http://groups-beta.google.com/groups?q=operation+must+use+updatable+query&qt_s=Search

you'll find people will often be more helpful if you tell them what
steps you have already taken to try and overcome the problem!

hope this helps
tim
 
D

DC

Ok sorry about that, I had had similar errors to this using old style
asp and remebered that it was realied to the IUSR_MACHINE accocunt
access, but this being ASP.net that ididnt make any difference. Adding
the ASPNET account with write previledges worked.

Thanks,
--
_______________________________________________

DC

"You can not reason a man out of a position he did not reach through reason"

"Don't use a big word where a diminutive one will suffice."

"A man with a watch knows what time it is. A man with two watches is
never sure." Segal's Law
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top