views:

2174

answers:

4

hi friends i am creating crytal report frm the stored procedure ,this works fine when i pass one parameter but it shows an error "incorrect parameter " whn i pass two parameter my code is

 {
    ReportDocument reportDocument = new ReportDocument();
    ParameterField paramField = new ParameterField();
    ParameterFields paramFields = new ParameterFields();
    ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();

    paramField.Name = "@Dept";
    paramDiscreteValue.Value = TextBox1.Text.ToString();
    paramField.CurrentValues.Add(paramDiscreteValue);
    paramFields.Add(paramField);

    paramField.Name = "@Name";
    paramDiscreteValue.Value = TextBox2.Text.ToString();
    paramField.CurrentValues.Add(paramDiscreteValue);
    paramFields.Add(paramField);

    CrystalReportViewer1.ParameterFieldInfo = paramFields;
    reportDocument.Load(Server.MapPath("CrystalReport.rpt"));
    CrystalReportViewer1.ReportSource = reportDocument;
    reportDocument.SetDatabaseLogon("sa", "sa", "OPWFMS-7KYGZ7SB", "test");

}

plz let me knw any chnges

+1  A: 

Make sure the actual data being passed in the parameters isn't being implicitly converted to the wrong data type. Like if you're passing a numeric ID for the @Dept parameter, make sure the data type of the input parameter that expects to receive the value is also a numeric type.

Darth Continent
no, everything is correct
+3  A: 

You need to create new parameterField and value for both parameters. Your current code adds parameter, modifies it (change name and value) and adds same object again. This should be correct:

 {
ReportDocument reportDocument = new ReportDocument();

ParameterFields paramFields = new ParameterFields();
// ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();

ParameterField paramField = new ParameterField();
ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();
paramField.Name = "@Dept";
paramDiscreteValue.Value = TextBox1.Text.ToString();
paramField.CurrentValues.Add(paramDiscreteValue);
paramFields.Add(paramField);

paramField = new ParameterField(); // <-- This line is added
paramDiscreteValue = new ParameterDiscreteValue();  // <-- This line is added
paramField.Name = "@Name";
paramDiscreteValue.Value = TextBox2.Text.ToString();
paramField.CurrentValues.Add(paramDiscreteValue);
paramFields.Add(paramField);

CrystalReportViewer1.ParameterFieldInfo = paramFields;
reportDocument.Load(Server.MapPath("CrystalReport.rpt"));
CrystalReportViewer1.ReportSource = reportDocument;
reportDocument.SetDatabaseLogon("sa", "sa", "OPWFMS-7KYGZ7SB", "test");

}

EDIT: Error mentioned in comment is probably because there are two definitions of variable paramField or paramDiscreteValue in code. In one c# method you can't define variable with same name more than one time. Try code above as it is written and if you are still getting compiler error, please paste here full error text.

zendar
no its not working there is error that paramFieldparamDiscreteValueare already defined
Yup. There are two definitions of ParameterDiscreteValue. I'll comment extra line.
zendar
plz write the code agn i m nt getting youthnks
same error "the incorrect parameter"
This is not same error as before? In which line? Please post full error text.Is this compiler or runtime error?
zendar
this is not complile time error report is generating but in report there is written "in correct parameter"
Then you should check if parameter names in code (@Dept and @Name) are the same as parameter names in Crystal report. Also check types - both parameters in code are strings, see that report also expects string values.
zendar
i hav lot of times the report is generating for one parameter there is no problemanyway thanks for help
hi i m realy sorrywhn i add nre parameter in my code ,i thought it would directly add in report but it wasnt there now everythng is fine sorry agn
+1  A: 

Try to create the ParameterField before each parameter that you add to the report:

paramField = new ParameterField();
paramDiscreteValue.Value = ...
...
Nelson Reis
no its not working there is error that paramFieldand paramDiscreteValueare already defined
A: 

Try this it is a little more concise

{    
  ReportDocument reportDocument = new ReportDocument();
  reportDocument.SetParameterValue("@Dept", TextBox1.Text.ToString());    
  reportDocument.SetParameterValue("@Name", TextBox2.Text.ToString());

  CrystalReportViewer1.ParameterFieldInfo = paramFields;
  reportDocument.Load(Server.MapPath("CrystalReport.rpt"));
  CrystalReportViewer1.ReportSource = reportDocument;
  reportDocument.SetDatabaseLogon("sa", "sa", "OPWFMS-7KYGZ7SB", "test");
}
John Hunter