一直提示返回的 数据为NULL。

s63403048 2011-10-11 04:09:23
麻烦各位帮我看看这里到底是哪里错了。
感觉上是
 string strsql = string.Format("select(CommodityName='{0}',SortID='{1}',CommodityPrice='{2}',IsDiscount='{3}',ReducedPrice='{4}')from commodity", txtName.Text, txtType.SelectedValue.ToString(), nudPrice.Value, IsDiscount, numericUpDown1.Value);
这句错了


下面是全部的代码。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SuperMarket
{
public partial class CommoDityUpdate : Form
{
public int CommodityID;
public string CommodityName;
public string SortName;
public decimal CommodityPrice;
public bool IsDiscount;
public decimal ReducedPrice;
DataSet ds;
SqlConnection conn = new SqlConnection("server=.;database=SuperMarket;uid=sa;pwd=Shaona520;");
public CommoDityUpdate()
{
InitializeComponent();
}
private void CommoDityUpdate_Load(object sender, EventArgs e)
{
ds = new DataSet();
string strsql = "select SortName,SortID from CommoditySort";
SqlDataAdapter adapter = new SqlDataAdapter(strsql, conn);
adapter.Fill(ds, "CommoditySort");
txtType.DataSource = ds.Tables["CommoditySort"];
txtType.DisplayMember = "SortName";
txtType.ValueMember = "SortID";
txtType.SelectedIndex = 0;


}

private void nudPrice_ValueChanged(object sender, EventArgs e)
{
if (chkSole.Checked == false)
{
numericUpDown1.Enabled = false;
numericUpDown1.Value = nudPrice.Value;
}
else
{ numericUpDown1.Enabled = true; }
}

private void button2_Click(object sender, EventArgs e)
{
this.Close();
}

private void button1_Click(object sender, EventArgs e)
{
if (checkUserInput())
{
if (checkedUserInfotoSql())
{
UpdateUser();
}
else
{ MessageBox.Show("您的信息和数据库信息不一致;"); }


}
}

private bool checkedUserInfotoSql()
{
if (checkUserDATA())
{
if (nudPrice.Value != 0)
{
return true;

}


}
return false;
}

private bool checkUserDATA()
{
try
{
conn.Open();
string strsql = string.Format("select(CommodityName='{0}',SortID='{1}',CommodityPrice='{2}',IsDiscount='{3}',ReducedPrice='{4}')from commodity", txtName.Text, txtType.SelectedValue.ToString(), nudPrice.Value, IsDiscount, numericUpDown1.Value);
SqlCommand cmd = new SqlCommand(strsql, conn);
int count = cmd.ExecuteNonQuery();
if (count > 0)
{
return true;
}
else
{
return false;
}

}
catch (Exception e1)
{
MessageBox.Show(e1.Message);
return false;
}
finally
{
conn.Close();
}
}

private void UpdateUser()
{
int IsDiscount;
if (chkSole.Checked == false)
{
IsDiscount = 0;
}
else
{ IsDiscount = 1; }
try
{
conn.Open();
string strsql = string.Format("Update commodity set(CommodityName,SortID,CommodityPrice,IsDiscount,ReducedPrice)values('{0}','{1}','{2}','{3}','{4}')",txtName.Text, txtType.SelectedValue.ToString(), nudPrice.Value, IsDiscount, numericUpDown1.Value);
SqlCommand cmd = new SqlCommand(strsql, conn);
int count = cmd.ExecuteNonQuery();
if (count > 0)
{
MessageBox.Show("修改成功");
}
else {
MessageBox.Show("修改失败");
}

}
catch (Exception e1) { MessageBox.Show(e1.Message); }
finally {
conn.Close();
}
}

private bool checkUserInput()
{
if (txtName.Text == null)
{
MessageBox.Show("请输入用户名,用户名不允许为空");
txtName.Focus();
return false;
}
else if (nudPrice.Value == 0)
{
MessageBox.Show("对不起预售价格不允许为空");
nudPrice.Focus();
}
return true;
}
}
}
...全文
316 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
sdl2005lyx 2011-10-12
  • 打赏
  • 举报
回复
靠,为NULL,你还不知道出错原因!说明赋值方式有问题:
string strsql = string.Format(@"update Commodity
set CommodityName='{0}',SortID='{1}',CommodityPrice='{2}',IsDiscount='{3}',ReducedPrice='{4}'
where CommodityID='{5}'",txtName.Text, txtType.SelectedValue.ToString(),nudPrice.Value, IsDiscount, numericUpDown1.Value,CommodityID);

这句话有问题,改成这样:
string strsql = string.Format("update Commodity
set CommodityName='{0}',SortID={1},CommodityPrice={2},IsDiscount={3},ReducedPrice={4}
where CommodityID={5}",txtName.Text, txtType.SelectedValue.ToString(),nudPrice.Value, IsDiscount, numericUpDown1.Value,CommodityID);

数据库字段不是字符串类型的,不要使用单引号‘ ’!
s63403048 2011-10-12
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 sdl2005lyx 的回复:]

那你strsql 运行的实际值贴出来。。。
[/Quote]
为NULL啊。
sdl2005lyx 2011-10-11
  • 打赏
  • 举报
回复
那你strsql 运行的实际值贴出来。。。
s63403048 2011-10-11
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 sdl2005lyx 的回复:]

楼主,你把strsql 的实际值,直接拷贝到数据库查询分析器,执行一下,问题就一目了然了。。。
[/Quote]


弄了。
但是发现还是有问题。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SuperMarket
{
public partial class CommoDityUpdate : Form
{
public int CommodityID;
public string CommodityName;
public string SortName;
public decimal CommodityPrice;
public bool IsDiscount;
public decimal ReducedPrice;
DataSet ds;
SqlConnection conn = new SqlConnection("server=.;database=SuperMarket;uid=sa;pwd=Shaona520;");
public CommoDityUpdate()
{
InitializeComponent();
}
private void CommoDityUpdate_Load(object sender, EventArgs e)
{
ds = new DataSet();
string strsql = "select SortName,SortID from CommoditySort";
SqlDataAdapter adapter = new SqlDataAdapter(strsql, conn);
adapter.Fill(ds, "CommoditySort");
txtType.DataSource = ds.Tables["CommoditySort"];
txtType.DisplayMember = "SortName";
txtType.ValueMember = "SortID";
txtType.SelectedIndex = 0;


}

private void nudPrice_ValueChanged(object sender, EventArgs e)
{
if (chkSole.Checked == false)
{
numericUpDown1.Enabled = false;
numericUpDown1.Value = nudPrice.Value;
}
else
{ numericUpDown1.Enabled = true; }
}

private void button2_Click(object sender, EventArgs e)
{
this.Close();
}

private void button1_Click(object sender, EventArgs e)
{
if (checkUserInput())
{
if (checkedUserInfotoSql())
{
UpdateUser();
}
else
{ MessageBox.Show("您的信息和数据库信息不一致;"); }


}
}

private bool checkedUserInfotoSql()
{
if (checkUserDATA())
{
if (nudPrice.Value != 0)
{
return true;

}


}
return false;
}

private bool checkUserDATA()
{
try
{
if (txtName.Text == CommodityName && txtType.Text == SortName && nudPrice.Value == CommodityPrice && IsDiscount == chkSole.Checked && numericUpDown1.Value == ReducedPrice)
{
return false;
}
else
{
return true;
}

}
catch (Exception e1)
{
MessageBox.Show(e1.Message);
return false;
}
finally
{
conn.Close();
}
}

private void UpdateUser()
{
int IsDiscount;
if (chkSole.Checked == false)
{
IsDiscount = 0;
}
else
{ IsDiscount = 1; }
try
{
conn.Open();
string strsql = string.Format(@"update Commodity
set CommodityName='{0}',SortID='{1}',CommodityPrice='{2}',IsDiscount='{3}',ReducedPrice='{4}'
where CommodityID='{5}'",txtName.Text, txtType.SelectedValue.ToString(),nudPrice.Value, IsDiscount, numericUpDown1.Value,CommodityID);
SqlCommand cmd = new SqlCommand(strsql, conn);
int count = cmd.ExecuteNonQuery();
if (count > 0)
{
MessageBox.Show("修改成功");
}
else {
MessageBox.Show("修改失败");
}

}
catch (Exception e1) { MessageBox.Show(e1.Message); }
finally {
conn.Close();
}
}

private bool checkUserInput()
{
if (txtName.Text == null)
{
MessageBox.Show("请输入用户名,用户名不允许为空");
txtName.Focus();
return false;
}
else if (nudPrice.Value == 0)
{
MessageBox.Show("对不起预售价格不允许为空");
nudPrice.Focus();
}
return true;
}
}
}
s63403048 2011-10-11
  • 打赏
  • 举报
回复
解决了。

但是update的语句错了。

应该好解决了。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SuperMarket
{
public partial class CommoDityUpdate : Form
{
public int CommodityID;
public string CommodityName;
public string SortName;
public decimal CommodityPrice;
public bool IsDiscount;
public decimal ReducedPrice;
DataSet ds;
SqlConnection conn = new SqlConnection("server=.;database=SuperMarket;uid=sa;pwd=Shaona520;");
public CommoDityUpdate()
{
InitializeComponent();
}
private void CommoDityUpdate_Load(object sender, EventArgs e)
{
ds = new DataSet();
string strsql = "select SortName,SortID from CommoditySort";
SqlDataAdapter adapter = new SqlDataAdapter(strsql, conn);
adapter.Fill(ds, "CommoditySort");
txtType.DataSource = ds.Tables["CommoditySort"];
txtType.DisplayMember = "SortName";
txtType.ValueMember = "SortID";
txtType.SelectedIndex = 0;


}

private void nudPrice_ValueChanged(object sender, EventArgs e)
{
if (chkSole.Checked == false)
{
numericUpDown1.Enabled = false;
numericUpDown1.Value = nudPrice.Value;
}
else
{ numericUpDown1.Enabled = true; }
}

private void button2_Click(object sender, EventArgs e)
{
this.Close();
}

private void button1_Click(object sender, EventArgs e)
{
if (checkUserInput())
{
if (checkedUserInfotoSql())
{
UpdateUser();
}
else
{ MessageBox.Show("您的信息和数据库信息不一致;"); }


}
}

private bool checkedUserInfotoSql()
{
if (checkUserDATA())
{
if (nudPrice.Value != 0)
{
return true;

}


}
return false;
}

private bool checkUserDATA()
{
try
{
if (txtName.Text == CommodityName && txtType.Text == SortName && nudPrice.Value == CommodityPrice && IsDiscount == chkSole.Checked && numericUpDown1.Value == ReducedPrice)
{
return false;
}
else
{
return true;
}

}
catch (Exception e1)
{
MessageBox.Show(e1.Message);
return false;
}
finally
{
conn.Close();
}
}

private void UpdateUser()
{
int IsDiscount;
if (chkSole.Checked == false)
{
IsDiscount = 0;
}
else
{ IsDiscount = 1; }
try
{
conn.Open();
string strsql = string.Format("Update commodity set CommodityName,SortID,CommodityPrice,IsDiscount,ReducedPrice values('{0}','{1}','{2}','{3}','{4}')",txtName.Text, txtType.SelectedValue.ToString(), nudPrice.Value, IsDiscount, numericUpDown1.Value);
SqlCommand cmd = new SqlCommand(strsql, conn);
int count = cmd.ExecuteNonQuery();
if (count > 0)
{
MessageBox.Show("修改成功");
}
else {
MessageBox.Show("修改失败");
}

}
catch (Exception e1) { MessageBox.Show(e1.Message); }
finally {
conn.Close();
}
}

private bool checkUserInput()
{
if (txtName.Text == null)
{
MessageBox.Show("请输入用户名,用户名不允许为空");
txtName.Focus();
return false;
}
else if (nudPrice.Value == 0)
{
MessageBox.Show("对不起预售价格不允许为空");
nudPrice.Focus();
}
return true;
}
}
}
baysos 2011-10-11
  • 打赏
  • 举报
回复
断点,取出SQL语句放到查询分析器里查查看看有没有数据。
s63403048 2011-10-11
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hongbinghai 的回复:]

select CommodityName='"+txtName.Text+"' from CommoditySort
按照这样写写试试,或者是你查询的的内容数据库没有
[/Quote]

这个样子写是对的。

但是查询不到任何数据。
sdl2005lyx 2011-10-11
  • 打赏
  • 举报
回复
楼主,你把strsql 的实际值,直接拷贝到数据库查询分析器,执行一下,问题就一目了然了。。。
ysudongxi 2011-10-11
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 chenpeng0118 的回复:]
条件中间怎么能用逗号??

SELECT * FROM TABLE WHERE USERID='' AND USERPWD=''
[/Quote]
囧,俺错了,直接复制LZ的~
allen0118 2011-10-11
  • 打赏
  • 举报
回复
条件中间怎么能用逗号??

SELECT * FROM TABLE WHERE USERID='' AND USERPWD=''

zxckfc 2011-10-11
  • 打赏
  • 举报
回复
不要括号
ysudongxi 2011-10-11
  • 打赏
  • 举报
回复
你sql语句对吗?
String.Format("select * from tb where CommodityName='{0}',SortID='{1}',CommodityPrice='{2}',IsDiscount='{3}',ReducedPrice='{4}'",txtName.Text, txtType.SelectedValue.ToString(), nudPrice.Value, IsDiscount, numericUpDown1.Value)

hongbinghai 2011-10-11
  • 打赏
  • 举报
回复
select CommodityName='"+txtName.Text+"' from CommoditySort
按照这样写写试试,或者是你查询的的内容数据库没有
hsphsphsp 2011-10-11
  • 打赏
  • 举报
回复
select和(之间加个空格试一下?
三石-gary 2011-10-11
  • 打赏
  • 举报
回复
打个端点慢慢调试看。。

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧