我是这种模式的新手,我想知道为什么我会收到错误
“无法将dbnull转换为其他类型”
我知道我的代码不起作用,这就是为什么它会在_view上返回一些空值,任何帮助?我得到这个在线模型.CustomerID = _view.CustomerID;
Model
public class CustomerModel
{
private string _customerid;
public string CustomerID;
{
get { return _customerid; }
set { _customerid = value; }
}
private string _orderstatus;
public string OrderStatus
{
get { return _orderstatus; }
set { _orderstatus = value; }
}
public void UpdateOrderStatus()
{
var connSettings = ConfigurationManager.ConnectionStrings["DB"];
{
string CN = connSettings.ConnectionString;
MySqlConnection conn = new MySqlConnection(CN);
MySqlCommand cmd = new MySqlCommand("update customerorders set order_status = @value where customerid = @customerid", conn);
conn.Open();
cmd.Parameters.AddWithValue("@customerid", _customerid);
cmd.Parameters.AddWithValue("@value", _orderstatus);
cmd.ExecuteNonQuery();
}
}
}
Interface
public interface ICustomer
{
void SetPresenter(CustomerPresenter presenter);
void UpdateCustomerOrderStatus(CustomerModel record);
string CustomerID { get; set; }
string OrderStatus { get; set; }
}
Presenter
public class CustomerPresenter
{
ICustomer _view;
CustomerModel _model = new CustomerModel();
public void CustomerPresenter(ICustomer view)
{
_view = view;
}
private void updateCustomerOrderStatus(CustomerModel model)
{
model.CustomerID = _view.CustomerID;
model.OrderStatus = _view.OrderStatus;
}
public void Save()
{
updateCustomerOrderStatus(CustomerModel model)
this._view.UpdateCustomerOrderStatus(CustomerModel record);
}
}
CustomerForm
public partial class frmCustomer: Form, ICustomer
{
CustomerPresenter _presenter
public void SetPresenter(CustomerPresenter presenter)
{
_presenter = presenter
}
public string CustomerID
{
get { return this.txtCustomerID.Text; }
set { txtCustomerID.Text = value; }
}
public string OrderStatus
{
get { return this.txtOrderStatus.Text; }
set { txtOrderStatus.Text = value; }
}
public void UpdateCustomerOrderStatus(CustomerModel record)
{
record.CustomerID = txtCustomerID.Text;
record.OrderStatus = txtOrderStatus.Text;
}
private void btnSave_Click(object sender, EventArgs e)
{
_presenter.Save();
}
}
答案 0 :(得分:2)
您的_view
现已确实已正确初始化,但_view.CustomerID
始终为null
,因为您未在get
和set
内执行任何操作。因此,当您尝试使用get
时,它不会返回任何内容,这就是您获得此异常的原因。
答案 1 :(得分:0)
根据代码,_view
永远不会被初始化。
答案 2 :(得分:0)
您是否将ViewID隐藏在视图中,如果不将其隐藏在视图中,我认为它将修复错误