未处理 System.IndexOutOfRangeException
Message="无法找到表 1。"
Source="System.Data"
StackTrace:
在 System.Data.DataTableCollection.get_Item(Int32 index)
在 表.Form1.ReportShow() 位置 E:\mspj_table\表\表\Form1.cs:行号 102
在 表.Form1.Form1_Load(Object sender, EventArgs e) 位置 E:\mspj_table\表\表\Form1.cs:行号 25
在 System.Windows.Forms.Form.OnLoad(EventArgs e)
在 System.Windows.Forms.Form.OnCreateControl()
在 System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
在 System.Windows.Forms.Control.CreateControl()
在 System.Windows.Forms.Control.WmShowWindow(Message& m)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
在 System.Windows.Forms.ContainerControl.WndProc(Message& m)
在 System.Windows.Forms.Form.WmShowWindow(Message& m)
在 System.Windows.Forms.Form.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
在 System.Windows.Forms.Control.SetVisibleCore(Boolean value)
在 System.Windows.Forms.Form.SetVisibleCore(Boolean value)
在 System.Windows.Forms.Control.set_Visible(Boolean value)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 表.Program.Main() 位置 E:\mspj_table\表\表\Program.cs:行号 15
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException:
Message="无法找到表 1。"
Source="System.Data"
StackTrace:
在 System.Data.DataTableCollection.get_Item(Int32 index)
在 表.Form1.ReportShow() 位置 E:\mspj_table\表\表\Form1.cs:行号 102
在 表.Form1.Form1_Load(Object sender, EventArgs e) 位置 E:\mspj_table\表\表\Form1.cs:行号 25
在 System.Windows.Forms.Form.OnLoad(EventArgs e)
在 System.Windows.Forms.Form.OnCreateControl()
在 System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
在 System.Windows.Forms.Control.CreateControl()
在 System.Windows.Forms.Control.WmShowWindow(Message& m)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
在 System.Windows.Forms.ContainerControl.WndProc(Message& m)
在 System.Windows.Forms.Form.WmShowWindow(Message& m)
在 System.Windows.Forms.Form.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
在 System.Windows.Forms.Control.SetVisibleCore(Boolean value)
在 System.Windows.Forms.Form.SetVisibleCore(Boolean value)
在 System.Windows.Forms.Control.set_Visible(Boolean value)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 表.Program.Main() 位置 E:\mspj_table\表\表\Program.cs:行号 15
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException:
{
dt.Rows.Add();
dt.Rows[i]["aa"] = ds.Tables[0][GZJL_Table].Rows[i]["aa"];
dt.Rows[i]["bb"] = ds.Tables[0][GZJL_Table].Rows[i]["bb"];
dt.Rows[i]["cc"] = ds.Tables[0][GZJL_Table].Rows[i]["cc"];
dt.Rows[i]["dd"] = ds.Tables[0][GZJL_Table].Rows[i]["dd"];
}
for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
{
dt.Rows.Add();
dt.Rows[i]["aaa"] = ds.Tables[1].Rows[i]["aaa"];
dt.Rows[i]["bbb"] = ds.Tables[1].Rows[i]["bbb"];
dt.Rows[i]["ccc"] = ds.Tables[1].Rows[i]["ccc"];
dt.Rows[i]["ddd"] = ds.Tables[1].Rows[i]["ddd"];
}代码相关的
if(ds!=null)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
dt.Rows.Add();
dt.Rows[i]["aa"] = ds.Tables[0][GZJL_Table].Rows[i]["aa"];
dt.Rows[i]["bb"] = ds.Tables[0][GZJL_Table].Rows[i]["bb"];
dt.Rows[i]["cc"] = ds.Tables[0][GZJL_Table].Rows[i]["cc"];
dt.Rows[i]["dd"] = ds.Tables[0][GZJL_Table].Rows[i]["dd"];
}
for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
{
dt.Rows.Add();
dt.Rows[i]["aaa"] = ds.Tables[1].Rows[i]["aaa"];
dt.Rows[i]["bbb"] = ds.Tables[1].Rows[i]["bbb"];
dt.Rows[i]["ccc"] = ds.Tables[1].Rows[i]["ccc"];
dt.Rows[i]["ddd"] = ds.Tables[1].Rows[i]["ddd"];
} }
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Windows.Forms;
using System.Data.SqlClient;namespace 表
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
ReportShow();
} //调用报表显示
private void ReportShow()
{
//声明报表
CrystalReport1 myRpt = new CrystalReport1();
//初始化报表内容
DataTable dt = new DataTable();
dt.Columns.Add("aa");
dt.Columns.Add("bb");
dt.Columns.Add("cc");
dt.Columns.Add("aaa");
dt.Columns.Add("bbb");
dt.Columns.Add("ccc");
//报表数据取得
DataSet ds = MSPJGetDataSet1();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
dt.Rows.Add(); dt.Rows[i]["aa"] = ds.Tables[0].Rows[i][0];
dt.Rows[i]["bb"] = ds.Tables[0].Rows[i]["bb"];
dt.Rows[i]["cc"] = ds.Tables[0].Rows[i]["cc"];
}
for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
{
dt.Rows.Add();
dt.Rows[i]["aaa"] = ds.Tables[1].Rows[i]["aaa"];
dt.Rows[i]["bbb"] = ds.Tables[1].Rows[i]["bbb"];
dt.Rows[i]["ccc"] = ds.Tables[1].Rows[i]["ccc"];
}
myRpt.SetDataSource(dt);
this.crystalReportViewer1.ReportSource = myRpt全部的了~~~
protected static string strConn = "server=localhost;user id=MSPJ;password=mspj;database=MSPJ";
//取得数据集
private DataSet MSPJGetDataSet1()
{
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
SqlCommand selCMD = new SqlCommand();
selCMD.Connection = conn;
selCMD.CommandText = "select * from [GRXX_Table] as a,[XXPX_Table] as b,[MSPJ_Table] as c where (a.[aaa]=b.[bb]) and (a.[aaa] = c.[ccc])";
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = selCMD;
da.Fill(ds);
conn.Close(); return ds;
}
private DataSet MSPJGetDataSet2()
{
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
SqlCommand selCMD = new SqlCommand();
selCMD.Connection = conn;
selCMD.CommandText = "select * from [GZJL_Table]";
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = selCMD;
da.Fill(ds);
conn.Close(); return ds;
} }
}最后的
怎么会查出来两个表呢你打印出ds.Tables.Count 看看
这样你返回的dataset里当然只有一张表啊!
写方法就是要通用点!