Ok, I'm using code-behind with a MySQL DB to update two tables. It updates the addressbook table fine but the device table won't update. I know its probably some stupid syntax error but I am not seeing it. To be sure I have the right variables set, I am using labels for the IDs. The ID numbers are correct. Can someone help? Thanks.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Data.Odbc;
public partial class editassign : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Session["aspnet"] = Request.QueryString[0];
int counting = GridView1.Rows.Count;
if (counting == 0)
{
GridView1.Visible = false;
Wizard1.Visible = true;
}
else
{
GridView1.Visible = true;
Wizard1.Visible = false;
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Session["contact_id"] = this.DropDownList1.SelectedItem.Value;
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
Session["device_id"] = this.DropDownList2.SelectedItem.Value;
Label1.Text = Convert.ToString(Session["aspnet"]);
Label2.Text = Convert.ToString(Session["contact_id"]);
Label3.Text = Convert.ToString(Session["device_id"]);
}
protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
{
OdbcConnection con = null;
OdbcCommand cmd = null;
try
{
con = new OdbcConnection();
con.ConnectionString = "Driver={MySQL ODBC 5.1 Driver};server=MyDBServer;database=MyDB;uid=MyUser;pwd=MyPass;";
cmd = new OdbcCommand();
cmd.Connection = con;
cmd.Parameters.Add("?aspnet_id", OdbcType.Int).Value = (Session["aspnet"] != null) ? Convert.ToInt32(Session["aspnet"]) : 1;
cmd.Parameters.Add("?contact_id", OdbcType.Int).Value = (Session["contact_id"] != null) ? Convert.ToInt32(Session["contact_id"]) : 0;
con.Open();
// This one works fine
cmd.CommandText = "update egw_addressbook set aspnet_id = ? where contact_id = ?;";
cmd.ExecuteNonQuery();
// These two don't. Even tried combining, no better.
cmd.Parameters.Add("?aspnet_id", OdbcType.Int).Value = (Session["aspnet"] != null) ? Convert.ToInt32(Session["aspnet"]) : 1;
cmd.Parameters.Add("?device_id", OdbcType.Int).Value = (Session["device_id"] != null) ? Convert.ToInt32(Session["device_id"]) : 0;
cmd.CommandText = "update ewise_device set aspnet_id = ? where device_id = ?;";
cmd.ExecuteNonQuery();
cmd.Parameters.Add("?contact_id", OdbcType.Int).Value = (Session["contact_id"] != null) ? Convert.ToInt32(Session["contact_id"]) : 1;
cmd.Parameters.Add("?device_id", OdbcType.Int).Value = (Session["device_id"] != null) ? Convert.ToInt32(Session["device_id"]) : 0;
cmd.CommandText = "update ewise_device set contact_id = ? where device_id = ?;";
cmd.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
}
}
Comment