I hope someone can help me.
My dataadapter will not update my database. Everything seems to work, and I get no errors of any kind. But my database is not being touched. I ran a sql profiler on it and there is no trace of my update. I can retrieve data just fine, but I cannot get it to update with my onclick event. Please just point me in the right direction, or any direction would also be cool.
Thank you.
Here is my code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
publicpartialclassWebAdmin_EditNews2 : System.Web.UI.Page
{
publicSqlConnection conn =newSqlConnection("Data Source=vnadbms01;initial catalog=vnawebdev; " +
"integrated security=true;persist security info=True;");
publicSqlDataAdapter da =newSqlDataAdapter();
publicDataSet ds =newDataSet();
publicDataTable dt =newDataTable();
protectedvoid Page_Load(object sender,EventArgs e)
{
if (!IsPostBack)
{
string MsgIDString = Session["MsgID"].ToString();
//Select Parameters:
SqlParameter SQLPMsgID =newSqlParameter("@.Msg_ID",SqlDbType.Int);
SQLPMsgID.Direction =ParameterDirection.Input;
SQLPMsgID.Value = MsgIDString;
da.SelectCommand =newSqlCommand();
//Set Select Commands
da.SelectCommand.Connection = conn;
da.SelectCommand.CommandText ="Messages_select";
da.SelectCommand.CommandType =CommandType.StoredProcedure;
da.SelectCommand.Parameters.Add(SQLPMsgID);
da.Fill(ds,"VNA_News");
FormView1.DataSource = ds;
FormView1.DataBind();
}
}
protectedvoid EditButton_Click(object sender,EventArgs e)
{
//Update parameters:
Label sqlprmMsgID = (Label)FormView1.FindControl("Msglbl");
SqlParameter SQLPMsgID2 =newSqlParameter("@.Msg_ID",SqlDbType.Int);
SQLPMsgID2.Direction =ParameterDirection.Input;
SQLPMsgID2.Value = sqlprmMsgID.Text;
TextBox sqlprmUsrID = (TextBox)FormView1.FindControl("User_IDTextBox");
SqlParameter SQLPDeptID =newSqlParameter("@.User_ID",SqlDbType.Int);
SQLPDeptID.Direction =ParameterDirection.Input;
SQLPDeptID.Value = sqlprmUsrID.Text;
TextBox sqlprmDept= (TextBox)FormView1.FindControl("Dept_IDTextBox");
SqlParameter SQLPUsrID =newSqlParameter("@.Dept_ID",SqlDbType.Int);
SQLPUsrID.Direction =ParameterDirection.Input;
SQLPUsrID.Value = sqlprmDept.Text;
TextBox sqlprmBody = (TextBox)FormView1.FindControl("Msg_bodyTextBox");
SqlParameter SQLPMsgBody =newSqlParameter("@.Msg_body",SqlDbType.Text);
SQLPMsgBody.Direction =ParameterDirection.Input;
SQLPMsgBody.Value = sqlprmBody.Text;
TextBox sqlprmMsgTitle = (TextBox)FormView1.FindControl("Msg_TitleTextBox");
SqlParameter SQLPMsgTitle =newSqlParameter("@.Msg_Title",SqlDbType.VarChar);
SQLPMsgTitle.Direction =ParameterDirection.Input;
SQLPMsgTitle.Value = sqlprmMsgTitle.Text;
TextBox sqlprmsts = (TextBox)FormView1.FindControl("stsTextBox");
SqlParameter SQLPMsgsts =newSqlParameter("@.sts",SqlDbType.Int);
SQLPMsgsts.Direction =ParameterDirection.Input;
SQLPMsgsts.Value = sqlprmsts.Text;
TextBox sqlprmexpdat = (TextBox)FormView1.FindControl("Exp_DatTextBox");
SqlParameter SQLPExpDat =newSqlParameter("@.Exp_Dat",SqlDbType.DateTime);
SQLPExpDat.Direction =ParameterDirection.Input;
SQLPExpDat.Value = sqlprmexpdat.Text;
//Set Update Commands
SqlConnection conn2 =newSqlConnection("Data Source=vnadbms01;initial catalog=vnawebdev; " +
"integrated security=true;persist security info=True;");
SqlDataAdapter dau =newSqlDataAdapter();
dau.UpdateCommand =newSqlCommand();
dau.UpdateCommand.Connection = conn2;
dau.UpdateCommand.CommandText ="Messages_Update";
dau.UpdateCommand.CommandType =CommandType.StoredProcedure;
dau.UpdateCommand.Parameters.Add(SQLPMsgID2);
dau.UpdateCommand.Parameters.Add(SQLPDeptID);
dau.UpdateCommand.Parameters.Add(SQLPMsgTitle);
dau.UpdateCommand.Parameters.Add(SQLPMsgBody);
dau.UpdateCommand.Parameters.Add(SQLPMsgsts);
dau.UpdateCommand.Parameters.Add(SQLPExpDat);
dau.UpdateCommand.Parameters.Add(SQLPUsrID);
dau.TableMappings.Add("VNA_News","VNA_News");
dau.Update(ds,"VNA_News");
I recommend you not to use SqlDataAdapter.Update to do data update to database.It is better to use SqlCommand.ExecuteNonQuery or SqlCommand.ExecuteScaler.If you want to do this,try to see this.
http://samples.gotdotnet.com/quickstart/howto/doc/adoplus/UpdateDataFromDB.aspx
http://p2p.wrox.com/topic.asp?TOPIC_ID=7537
Wish this help you.
|||Thank you for the info. After doing some reading I see why the dataadapter in a webform can be problematic. It sounds like it works great for windows forms. I am moving ahead with the sqlcommand.Executenonquery. That should hopefully do the trick. If not, I will try another method.
I spent way too much time on this. I should have been open to other methods for getting the job done. Reading also helps.
No comments:
Post a Comment