问题描述
我想在textbox1中编写我的sql代码,在button1的click事件上我应该得到写入的sql代码的报告,这样做我得到一个错误无法转换类型的对象'CrystalDecisions.CrystalReports.Engine.FieldObject '输入'CrystalDecisions.CrystalReports.Engine.TextObject'。我在这行上得到了这个错误MyText =((TextObject)objRpt.ReportDefinition.ReportObjects [i + 1]);
这里是我的代码:
i want to write my sql code in textbox1 and on click event of button1 i should get the report of the written sql code while doing this i get an error "Unable to cast object of type 'CrystalDecisions.CrystalReports.Engine.FieldObject' to type 'CrystalDecisions.CrystalReports.Engine.TextObject'." i get this error on this line " MyText = ((TextObject)objRpt.ReportDefinition.ReportObjects[i + 1]);"
here is my code:
CrystalReport1 objRpt = new CrystalReport1();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, System.EventArgs e)
{
SqlConnection cnn;
string connectionString = null;
string sql = null;
connectionString = "data source=Abc-pc;initial catalog=ICB;Integrated Security = true;";
cnn = new SqlConnection(connectionString);
cnn.Open();
sql = procesSQL();
SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
PremiumPolicyDataSet ds = new PremiumPolicyDataSet();
dscmd.Fill(ds, "EMP_INFO");
objRpt.SetDataSource(ds.Tables[1]);
crystalReportViewer1.ReportSource = objRpt;
crystalReportViewer1.Refresh();
}
public string procesSQL()
{
string sql = null;
string inSql = null;
string firstPart = null;
string lastPart = null;
int selectStart = 0;
int fromStart = 0;
string[] fields = null;
string[] sep = { "," };
int i = 0;
TextObject MyText;
inSql = textBox1.Text;
inSql = inSql.ToUpper();
selectStart = inSql.IndexOf("SELECT");
fromStart = inSql.IndexOf("FROM");
selectStart = selectStart + 6;
firstPart = inSql.Substring(selectStart, (fromStart - selectStart));
lastPart = inSql.Substring(fromStart, inSql.Length - fromStart);
fields = firstPart.Split(',');
firstPart = "";
for (i = 0; i <= fields.Length - 1; i++)
{
if (i > 0)
{
firstPart = firstPart + ", " + fields[i].ToString() + " AS COLUMN" + (i + 1);
firstPart.Trim();
MyText = (TextObject)objRpt.ReportDefinition.ReportObjects[i + 1];
MyText.Text = fields[i].ToString();
}
else
{
firstPart = firstPart + fields[i].ToString() + " AS COLUMN" + (i + 1);
firstPart.Trim();
MyText = ((TextObject)objRpt.ReportDefinition.ReportObjects[i + 1]);
MyText.Text = fields[i].ToString();
}
}
sql = "SELECT " + firstPart + " " + lastPart;
return sql;
}
}
任何帮助
Any Help
推荐答案
我想在textbox1中编写我的sql代码,并且在button1的click事件中我应该在执行此操作时获取编写的sql代码的报告我收到错误无法将类型为'CrystalDecisions.CrystalReports.Engine.FieldObject'的对象强制转换为'CrystalDecisions.CrystalReports.Engine.TextObject'。我在这一行上得到这个错误MyText =((TextObject)objRpt.ReportDefinition.ReportObjects [i + 1]);
i want to write my sql code in textbox1 and on click event of button1 i should get the report of the written sql code while doing this i get an error "Unable to cast object of type 'CrystalDecisions.CrystalReports.Engine.FieldObject' to type 'CrystalDecisions.CrystalReports.Engine.TextObject'." i get this error on this line " MyText = ((TextObject)objRpt.ReportDefinition.ReportObjects[i + 1]);"
你选择了错误的基于错误的索引(i + 1)。并查看 [ ^ ]了解更多详情。
You're selecting wrong object based wrong index(i+1). And check this thread[^] for more details.
MyText=(TextObject)objRpt.ReportDefinition.ReportObject[i];
而不是
instead of
MyText=(TextObject)objRpt.ReportDefinition.ReportObject[i+1];
也许在最后一项上引发错误。检查,但我没有测试过。
Maybe the error is raised on the last item. Check, but I haven't tested.
这篇关于动态水晶报告紧急帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!