Re: [Newbie Question] Updating a record problem.
- From: "Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 17 Jun 2005 10:50:41 -0400
DC,
Is there a reason that you are not using a data set and a data adapter?
Creating SQL like this is a REALLY bad idea. If I entered the following
into the tbID field:
1; drop table seminarlist
You would be in big trouble.
As for why it is not updating, are you sure that the id is valid that
you are updating?
I would recommend creating a typed data set and let it generate the
parameterized query for you. It will be safer, and quite honestly, it will
reduce any errors you might get from trying to dynamically generate the SQL
yourself.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
"DC" <sws99dsc@xxxxxxxxx> wrote in message
news:d8un3j$lk9$1@xxxxxxxxxxxxxxxxxxxxxx
> My update code seems to execute without error but doesnt write the data
> back to my database, am I leaving someting out? Do I need to tell the
> DataAdapter to update the database somewhere?
>
>
>
> private void btnUpdateSeminar_Click(object sender, System.EventArgs e)
> {
> string strConnectionString;
> strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data
> Source='C:\\Program Files\\Common Files\\ODBC\\Data
> Sources\\seminars.mdb';";
>
> string UpdateSemSql = "UPDATE SeminarList SET Speaker = '" +
> tbSpeaker.Text + "', SpeakerEmail = '" + tbSpeakerEmail.Text +
> "', SpeakerInstitution = '" + tbSpeakerInstitution.Text +
> "',SeminarTitle = '" + tbSeminarTitle.Text + "', SeminarDate = #" +
> tbSeminarDate.Text + "#, SeminarTime = '" + tbSeminarTime.Text +
> "',SeminarLocation = '" + tbSeminarLocation.Text + "', SeminarDetails = '"
> + tbSeminarDetails.Text + "', SeminarHost = '" + tbSeminarHost.Text +
> "',SeminarHostEmail = '" + tbSeminarHostEmail.Text + "', SeminarType = '"
> + tbSeminarType.Text + "', OrganisationNotes = '" +
> tbOrganisationNotes.Text +
> "' WHERE ID = " + tbID.Text;
>
> OleDbConnection Conn = new OleDbConnection(strConnectionString);
> OleDbCommand cmd = new OleDbCommand(UpdateSemSql, Conn);
>
> try
> {
> cmd.Connection.Open();
> int numRecordsAffected = cmd.ExecuteNonQuery();
> if (numRecordsAffected == 0)
> {
> Response.Write("Didnt update the record, an error has occured<BR><BR>");
> }
> else
> {
> Response.Redirect("main page url");
> }
> }
>
> catch(Exception ex)
> {
> Response.Write("There was an exception error" + ex + "<BR><BR>");
> }
>
> finally
> {
> cmd.Connection.Close();
> }
>
> }
> --
> _______________________________________________
>
> 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
>
.
- Follow-Ups:
- References:
- Prev by Date: Re: C# and C++ communication
- Next by Date: Thread synchronization
- Previous by thread: [Newbie Question] Updating a record problem.
- Next by thread: Re: [Newbie Question] Updating a record problem.
- Index(es):
Relevant Pages
|