还是未引用实例,找不到哪里错了,,
我看来看去,实在是不知道哪里错了~!还是没有实例
CORE的UBUIL部分
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace main.Core
{
public class DbUtil
{
public static string GetConnectionString()
{
string path = @"server=fx\sqlexpress;uid=sa;pwd=123;database=main";
return path;
}
public static DataSet GetDataSet(string table, string where, string orderBy)
{
string sql = "select * from " + table + " where " +where + " order by " + orderBy ;
SqlConnection conn = new SqlConnection(GetConnectionString());
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet,"tree");
return dataSet;
}
public static DataSet GetDataSet(int top, string table, string where, string orderBy)
{
string sql = "select top " + top.ToString() + " * from " + table + " where " + where + " order by " + orderBy;
SqlConnection conn = new SqlConnection(GetConnectionString());
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
return dataSet;
}
}
}
主窗体部分
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace main
{
public partial class shangpinxinxi : Form
{
public shangpinxinxi()
{
InitializeComponent();
InitTreeViewNodes();
}
private void InitTreeViewNodes()
{
TreeNode root = new TreeNode();
root.Text = "产品类型";
root.Tag = 0;
treeView1.Nodes.Add(root);
DataSet ds = Core.DbUtil.GetDataSet("tree", "编号>0", "编号 asc");
BindNode(ds.Tables[0], root);
}
//绑定树的每个节点
private void BindNode(DataTable treeTable, TreeNode parentNode)
{
DataRow[] rows = treeTable.Select("parentId=" + parentNode.Tag.ToString());
foreach (DataRow dr in rows)
{
TreeNode node = new TreeNode();
node.Text = dr["产品名称"].ToString();
node.Tag = dr["编号"];
parentNode.Nodes.Add(node);
BindNode(treeTable, node);
}
}
private void button9_Click(object sender, EventArgs e)
{
TreeNode node = treeView1.SelectedNode;
//重点在这一句,这里就体现了,要了解,和清楚,什么是传值引用和地址引用
//把当前选中的父节点传到目标编辑窗口里面,node 是一个对象实例,所以是地址引用
form1 f = new form1(node);
f.ShowDialog();
}
public void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
// TODO: 这行代码将数据加载到表“mainDataSet19.tree”中。您可以根据需要移动或移除它。
this.treeTableAdapter.Fill(this.mainDataSet19.tree);
if (e.Node.Parent != null)
{
button1.Enabled = false;
}
else
{
button1.Enabled = false;
}
}
private void button1_Click(object sender, EventArgs e)
{
}
private void shangpinxinxi_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“mainDataSet19.tree”中。您可以根据需要移动或移除它。
this.treeTableAdapter.Fill(this.mainDataSet19.tree);
}
private void treeBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.treeBindingSource.EndEdit();
this.treeTableAdapter.Update(this.mainDataSet19.tree);
}
}
}
--------------------编程问答--------------------
子窗体部分
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace main
{
public partial class form1 : Form
{
TreeNode _ParentNode;
int _InfoId = -1;
public void SetInfoId(int 编号)
{
_InfoId = 编号;
}
Core.InfoState _InfoState = main.Core.InfoState.NEW;
public void SetInfoState(Core.InfoState infoState)
{
_InfoState = infoState;
}
public form1()
{
InitializeComponent();
}
public form1(TreeNode parentNode)
{
InitializeComponent();
this._ParentNode = parentNode;
InitGridView(Int32.Parse(parentNode.Tag.ToString()));
InitControlState();
}
private void InitControlState()
{
switch (_InfoState)
{
case main.Core.InfoState.NEW:
button1.Enabled = true;
button2.Enabled = false;
break;
case main.Core.InfoState.MODIFY:
button1.Enabled = false;
button2.Enabled = true;
break;
}
}
private void GetInfo()
{
}
private void InitGridView(int 编号)
{
DataSet dataSet = Core.DbUtil.GetDataSet("tree", "编号=" + 编号.ToString(), "编号 desc");
dataGridView1.DataSource = dataSet.Tables[0];
}
public void form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“mainDataSet18.tree”中。您可以根据需要移动或移除它。
this.treeTableAdapter2.Fill(this.mainDataSet18.tree);
}
public void treeDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string sql = "INSERT INTO tree(编号,treeID,类别名称,产品名称,规格型号,parentId,备注) VALUES(@编号,@treeID,@类别名称,@产品名称,@规格型号, @parentId,@备注)";
SqlConnection conn = new SqlConnection(Core.DbUtil.GetConnectionString());
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add(new SqlParameter("@编号", SqlDbType.Int ));
cmd.Parameters["@编号"].Value = 编号TextBox.Text.Trim();
cmd.Parameters.Add(new SqlParameter("@treeID", SqlDbType.NVarChar));
cmd.Parameters["@treeID"].Value = treeIDTextBox.Text.Trim();
cmd.Parameters.Add(new SqlParameter("@类别名称", SqlDbType.NVarChar));
cmd.Parameters["@类别名称"].Value = 类别名称TextBox.Text.Trim();
cmd.Parameters.Add(new SqlParameter("@产品名称", SqlDbType.NVarChar));
cmd.Parameters["@产品名称"].Value = 产品名称TextBox.Text.Trim();
cmd.Parameters.Add(new SqlParameter("@规格型号", SqlDbType.NVarChar));
cmd.Parameters["@规格型号"].Value = 规格型号TextBox.Text.Trim();
cmd.Parameters.Add(new SqlParameter("@parentId", SqlDbType.Int));
cmd.Parameters["@parentId"].Value = parentIdTextBox.Text.Trim();
cmd.Parameters.Add(new SqlParameter("@备注", SqlDbType.NVarChar));
cmd.Parameters["@备注"].Value = 备注TextBox.Text.Trim();
int i = 0;
try
{
conn.Open();
i = cmd.ExecuteNonQuery();
}
catch (Exception)
{
}
finally
{
conn.Close();
}
if (i > 0)
{
DataSet ds = Core.DbUtil.GetDataSet(1, "tree", "parentId=" + _ParentNode.Tag.ToString(), " 编号 desc");
int 编号= Int32.Parse(ds.Tables[0].Rows[0]["编号"].ToString());
//在这里,把添加节点
TreeNode node = new TreeNode();
node.Text = 产品名称TextBox.Text.Trim();
node.Tag = 编号;
_ParentNode.Nodes.Add(node);
InitGridView(Int32.Parse(_ParentNode.Tag.ToString()));
_ParentNode.Expand();
}
}
}
}
--------------------编程问答-------------------- 晕。。。。。。。。 --------------------编程问答-------------------- :(回答晕是没分的 --------------------编程问答-------------------- 这么一大坨的Source
别人怎么看啊?
错误很简单
就是有的对象没有实例化
你可以单步跟踪调试一下就知道了~~~~~ --------------------编程问答-------------------- 是啊 出错的地方察一下变量的值就知道了~~~~~ --------------------编程问答-------------------- 出错在什么地方。 --------------------编程问答--------------------
出错在这个位置。private void InitGridView(int 编号)
{
DataSet dataSet = Core.DbUtil.GetDataSet("tree", "编号=" + 编号.ToString(), "编号 desc");
dataGridView1.DataSource = dataSet.Tables[0];
}
我扫了一下,
dataGridView1是null
dataSet的是{System.Data.DataSet}
dataSet.Tablesr 的是{System.Data.DataSet.collection} --------------------编程问答-------------------- 1楼应该解决问题了吧,建议LZ这样的问题多用用调试,加个断点,然后F10和F11用好了,再配合监视,基本上问题都能自己解决了 --------------------编程问答-------------------- 我靠,你每次打开读取数据的时候连接对象都不用打开就行?!
connection.open()貌似你都没用过 --------------------编程问答-------------------- 每次读数据前con先open,读完后再close掉 --------------------编程问答-------------------- 就你的代码来看Core.InfoState这是从那里来的
--------------------编程问答-------------------- dataGridView1.DataSource = dataSet.Tables[0];
dataGridView1.DataBind(); --------------------编程问答-------------------- 楼上的大哥 没记错的话SqlDataAdapter 是自动打开关闭的 --------------------编程问答-------------------- 就你的代码来看Core.InfoState这是从那里来的 是从这里来的
using System;
using System.Collections.Generic;
using System.Text;
namespace main.Core
{
/// <summary>
/// 信息当前的状态
/// </summary>
public enum InfoState
{
/// <summary>
/// 新增
/// </summary>
NEW,
/// <summary>
/// 修改
/// </summary>
MODIFY
}
}
* 等 级:
发表于:2007-12-18 15:48:0812楼 得分:0
dataGridView1.DataSource = dataSet.Tables[0];
dataGridView1.DataBind();这么做是不行的
补充:.NET技术 , C#