I know its easily said, but harder written if your a NEWBIE like me, and I am sure users on here will tear this apart, but I've done it a way thats not correct but only one button!
This is my submit button.
private void btnSubmit_Click(object sender, System.EventArgs e)<br />
{<br />
<br />
bool bNoError=true;<br />
<br />
OleDbConnection con = new OleDbConnection(strConnection);<br />
string strSQL;<br />
DataSet ds = new DataSet();<br />
<br />
strSQL="SELECT Top 1 [q1], [q2], [q3], [q4], [q5], [q6], [q7], [q8],"+<br />
" [q9], [q10], [q11], [q12], [q13], [q14], [q15],"+<br />
" [q16], [q17], [q18], [q19], [q20],"+<br />
" [q21], [q23], [q24], [q26], [q28], [q34], "+<br />
" [q35], [q38], [q39], [q40], [q42], [q43], [q44], [q45], [q46], [q47], [q48], [q51], [q52], [q53], [q54], [q57], [q58], [q61], [q62], [q63], [q66], [q67], [q70], "+<br />
" [q71], [q72], [q73], [q74], [q75], [q84], [q85], [q86], [q87],"+<br />
" [q88], [q89], [q90], [q91], [q98], [q99], [q100], [q101],"+" [q102], [q103], [q104], [TCAIMSSite],"+<br />
" [DTG_Submit], [LoginID], [TestPhase], [Final] FROM t_EOT_User";<br />
OleDbDataAdapter da = new OleDbDataAdapter(strSQL,con); <br />
try<br />
{<br />
da.Fill(ds);<br />
DataTable mytable =ds.Tables[0];<br />
DataRow myRow ; <br />
myRow = mytable.NewRow();<br />
string n ;<br />
foreach(DataColumn c in mytable.Columns)<br />
{<br />
n = c.ColumnName;<br />
if(n=="ID")<br />
{<br />
continue;<br />
}<br />
myRow[n] =ControlTypeCheck.CheckControlType(Page.FindControl(n),n);<br />
}<br />
mytable.Rows.Add(myRow);<br />
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);<br />
int count1 =da.Update(ds);<br />
}<br />
catch(Exception err)<br />
{<br />
string s= err.Message;<br />
bNoError=false;<br />
UpdateData(); <br />
}<br />
finally<br />
{<br />
con.Close();<br />
}<br />
<br />
if(bNoError)<br />
{<br />
Response.Redirect("../Common/DataSubmitted.aspx");<br />
}<br />
else<br />
HandleErrors();<br />
}
If you look down in my catch, if there is an error (IE cant do it because it would create a duplicate record), then it supposed to give my an error message, what I did was throw in my update command there, so after it tried select statement if it couldnt do that it would try my update statment. Below is my update statment.
private void UpdateData()<br />
{<br />
<br />
bool bNoError=true;<br />
<br />
OleDbConnection con = new OleDbConnection(strConnection);<br />
string strSQL;<br />
DataSet ds = new DataSet();<br />
<br />
strSQL="UPDATE t_EOT_User set [q1]='"+q1.SelectedValue+"', [q2]='"+q2.Text+"',"+<br />
" [q3]='"+q3.SelectedValue+"', [q4]='"+q4.Text+"', [q5]='"+q5.SelectedValue+"',"+<br />
" [q6]='"+q6.Text+"', [q7]='"+q7.SelectedValue+"', [q8]='"+q8.Text+"',"+<br />
" [q9]='"+q9.SelectedValue+"', [q10]='"+q10.Text+"',"+<br />
" [q11]='"+q11.SelectedValue+"', [q12]='"+q12.Text+"',"+<br />
" [q13]='"+q13.SelectedValue+"', [q14]='"+q14.Text+"',"+<br />
" [q15]='"+q15.Text+"', [q16]='"+q16.Text+"',"+<br />
" [q17]='"+q17.Text+"', [q18]='"+q18.Text+"',"+<br />
" [q19]='"+q19.SelectedValue+"', [q20]='"+q20.SelectedValue+"',"+<br />
" [q21]='"+q21.SelectedValue+"', [q23]='"+q23.Text+"',"+<br />
" [q24]='"+q24.SelectedValue+"', [q26]='"+q26.Text+"',"+<br />
" [q28]='"+q28.Text+"', [q34]='"+q34.SelectedValue+"',"+<br />
" [q35]='"+q35.Text+"', [q38]='"+q38.SelectedValue+"',"+<br />
" [q39]='"+q39.Text+"', [q40]='"+q40.SelectedValue+"',"+<br />
" [q42]='"+q42.Text+"', [q43]='"+q43.SelectedValue+"',"+<br />
" [q44]='"+q44.Text+"', [q45]='"+q45.SelectedValue+"',"+<br />
" [q46]='"+q46.Text+"', [q47]='"+q47.SelectedValue+"',"+<br />
" [q48]='"+q48.Text+"', [q51]='"+q51.SelectedValue+"',"+<br />
" [q52]='"+q52.Text+"', [q53]='"+q53.SelectedValue+"',"+<br />
" [q54]='"+q54.Text+"', [q57]='"+q57.SelectedValue+"',"+<br />
" [q58]='"+q58.Text+"', [q61]='"+q61.Text+"',"+<br />
" [q62]='"+q62.Text+"', [q63]='"+q63.SelectedValue+"',"+<br />
" [q66]='"+q66.SelectedValue+"', [q67]='"+q67.Text+"',"+<br />
" [q70]='"+q70.SelectedValue+"', [q71]='"+q71.Text+"',"+<br />
" [q72]='"+q72.SelectedValue+"', [q73]='"+q73.Text+"',"+<br />
" [q74]='"+q74.SelectedValue+"', [q75]='"+q75.Text+"',"+<br />
" [q84]='"+q84.SelectedValue+"', [q85]='"+q85.Text+"',"+<br />
" [q86]='"+q86.SelectedValue+"', [q87]='"+q87.Text+"',"+<br />
" [q88]='"+q88.SelectedValue+"', [q89]='"+q89.Text+"',"+<br />
" [q90]='"+q90.Text+"', [q91]='"+q91.Text+"',"+<br />
" [q98]='"+q98.SelectedValue+"', [q99]='"+q99.SelectedValue+"',"+<br />
" [q100]='"+q100.Text+"', [q101]='"+q101.SelectedValue+"',"+<br />
" [q102]='"+q102.Text+"', [q103]='"+q103.SelectedValue+"',"+<br />
" [q104]='"+q104.Text+"', [Final]='"+Final.SelectedValue+"' WHERE RecNum="+RecNum.Text;<br />
<br />
OleDbDataAdapter da = new OleDbDataAdapter(strSQL,con); <br />
try<br />
{<br />
OleDbCommand cmd = con.CreateCommand();<br />
cmd.CommandText=strSQL;<br />
con.Open();<br />
cmd.ExecuteNonQuery();<br />
}<br />
catch(Exception err)<br />
{<br />
string s= err.Message;<br />
bNoError=false;<br />
}<br />
finally<br />
{<br />
con.Close();<br />
}<br />
<br />
if(bNoError)<br />
{<br />
Response.Redirect("../Common/DataSubmitted.aspx");<br />
}<br />
else<br />
HandleErrors();<br />
}
|