问题描述
我在这里收到错误:
cmd.CommandText = "select * from AddProduct_db where Product_Name='" + comboBox1.SelectedItem.ToString() + "'";
消息显示 - 对象引用未设置为对象的实例
private void comboBox1_SelectedIndexChanged(object sender,EventArgs e)
{
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText =select * from AddProduct_db where Product_Name ='+ comboBox1.SelectedItem.ToString()+';
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
foreach(dt.Rows中的DataRow dr)
{
textBox1.Text = dr [ Product_Name]。ToString();
pdescription.Text = dr [Product_Description]。ToString();
punits.Text = dr [Product_Units]。ToString();
pqty.Text = dr [Product_Stocks]。ToString();
}
con.Close();
}
我尝试过:
private void AddCart_Click(object发件人,EventArgs e)
{
if(IsValidated())
{
CartItem item = new CartItem ()
{
ProductName = comboBox1.Text,
描述= pdescription.Text,
ProductUnit = punits .Text,
股票= Convert.ToInt32(pqty.Text.Trim()),
UnitPrice = Convert.ToDecimal(price.Text.Trim()),
总计amount = Convert.ToInt16(pqty.Text.Trim())* Convert.ToDecimal(price.Text.Trim())
};
BillCart.Add(item );
BilldataGridView.DataSource = null;
BilldataGridView.DataSource = BillCart;
decimal TotalAmount = BillCart.Sum(x => x.Totalamount);
Totalamt.Text = TotalAmount.ToString();
decimal SalesTax =(12 * TotalAmount)/ 100;
GST12.Text = SalesTax.ToString();
十进制GrandTotals = TotalAmount + SalesTax;
GrandTotal .Text = GrandTotals.ToString();
pdescription.Clear();
punits.Clear();
pqty.Clear();
price.Clear();
comboBox1.SelectedIndex = -1;
}
}
private bool IsValidated()
{
if(cname.Text.Trim()== string.Empty )
{
MessageBox.Show(客户名称是必需的。,错误,MessageBoxButtons.OK,MessageBoxIcon.Error);
cname.Focus();
返回false;
}
if(comboBox1.SelectedIndex == -1 )
{
MessageBox.Show(选择产品名称。,错误,MessageBoxButtons.OK,MessageBoxIcon.Error);
comboBox1.Focus();
返回false;
}
if(comboBox2.SelectedIndex == - 1)
{
MessageBox.Show(选择付款模式。,错误,MessageBoxButtons.OK,MessageBoxIcon.Error);
comboBox2.Focus();
返回false;
}
if(pdescription.Text.Trim()== string.Empty)
{
MessageBox.Show(需要说明。 ,错误,MessageBoxButtons.OK,MessageBoxIcon。错误);
pdescription.Focus();
返回false;
}
if(punits.Text.Trim()== string.Empty)
{
MessageBox.Show(Units Required。,Error,MessageBoxButtons .OK,MessageBoxIcon.Error);
punits.Focus();
返回false;
}
if(pqty.Text.Trim()== string.Empty)
{
MessageBox.Show(数量不应该为空。 ,Error,MessageBoxButtons.OK,MessageBoxIcon.Error);
pqty.Focus();
返回false;
}
else
{
int tempQuantity;
bool isNumeric = int.TryParse(pqty.Text.Trim() ,out tempQuantity);
if(!isNumeric)
{
MessageBox.Show(Quantity should be integer value。,Error,MessageBoxButtons.OK,MessageBoxIcon.Error);
pqty.Clear();
pqty.Focus();
返回false;
}
}
if(price.Text.Trim()== string。空)
{
MessageBox.Show(产品价格不应为空白。,错误,MessageBoxButtons.OK,MessageBoxIcon.Error);
price.Focus();
返回false;
}
其他
{
十进制n;
bool isDecimal = decimal.TryParse(price.Text.Trim(),out n);
if(!isDecimal)
{
MessageBox.Show(产品价格应为数字值。,错误,MessageBoxButtons.OK,MessageBoxIcon.Error);
price.Clear();
price.Focus();
返回false;
}
返回true;
}
}
Message shows-Object reference not set to an instance of an object
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from AddProduct_db where Product_Name='" + comboBox1.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
textBox1.Text = dr["Product_Name"].ToString();
pdescription.Text = dr["Product_Description"].ToString();
punits.Text = dr["Product_Units"].ToString();
pqty.Text = dr["Product_Stocks"].ToString();
}
con.Close();
}
What I have tried:
private void AddCart_Click(object sender, EventArgs e)
{
if (IsValidated())
{
CartItem item = new CartItem()
{
ProductName = comboBox1.Text,
Description = pdescription.Text,
ProductUnit = punits.Text,
Stock = Convert.ToInt32(pqty.Text.Trim()),
UnitPrice = Convert.ToDecimal(price.Text.Trim()),
Totalamount = Convert.ToInt16(pqty.Text.Trim()) * Convert.ToDecimal(price.Text.Trim())
};
BillCart.Add(item);
BilldataGridView.DataSource = null;
BilldataGridView.DataSource = BillCart;
decimal TotalAmount = BillCart.Sum(x => x.Totalamount);
Totalamt.Text = TotalAmount.ToString();
decimal SalesTax = (12 * TotalAmount) / 100;
GST12.Text = SalesTax.ToString();
decimal GrandTotals = TotalAmount + SalesTax;
GrandTotal.Text = GrandTotals.ToString();
pdescription.Clear();
punits.Clear();
pqty.Clear();
price.Clear();
comboBox1.SelectedIndex = -1;
}
}
private bool IsValidated()
{
if (cname.Text.Trim() == string.Empty)
{
MessageBox.Show("Customer Name is required.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
cname.Focus();
return false;
}
if (comboBox1.SelectedIndex == -1)
{
MessageBox.Show("Choose Product Name.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
comboBox1.Focus();
return false;
}
if (comboBox2.SelectedIndex == -1)
{
MessageBox.Show("Choose Payment Mode.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
comboBox2.Focus();
return false;
}
if (pdescription.Text.Trim() == string.Empty)
{
MessageBox.Show("Description required.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
pdescription.Focus();
return false;
}
if (punits.Text.Trim() == string.Empty)
{
MessageBox.Show("Units Required.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
punits.Focus();
return false;
}
if (pqty.Text.Trim() == string.Empty)
{
MessageBox.Show("Quantity should not blank.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
pqty.Focus();
return false;
}
else
{
int tempQuantity;
bool isNumeric = int.TryParse(pqty.Text.Trim(), out tempQuantity);
if (!isNumeric)
{
MessageBox.Show("Quantity should be integer value.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
pqty.Clear();
pqty.Focus();
return false;
}
}
if (price.Text.Trim() == string.Empty)
{
MessageBox.Show("Product Price should not blank.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
price.Focus();
return false;
}
else
{
decimal n;
bool isDecimal = decimal.TryParse(price.Text.Trim(), out n);
if (!isDecimal)
{
MessageBox.Show("Product Price should be Numeric value.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
price.Clear();
price.Focus();
return false;
}
return true;
}
}
推荐答案
这篇关于如何解决未设置为object.in ASP.NET实例的对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!