Can't see parameters using dataset with crystal reports - asp.net

I've been dealing with this problem for more than 6 months, when I create a dataset and inside de dataset I call a stored procedure and then call this dataset into my report (crystal reports) I can't see the parameter fields:
I try to create the parameters in a sqlcommand and then add the parameters to my report using
rpt.setParameters("#a","sample");
but I got an error.
What could I do? please help me, I'm very desperated and angustied with this
My code (Sorry if it's in spanish) :
Dim cmd As New SqlCommand("prd_generarReporteOP", cn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("#idop", SqlDbType.Int).Value = CInt(Session("idop"))
cmd.Parameters.Add("#producto", SqlDbType.VarChar).Value = ""
Dim da As New SqlDataAdapter da.SelectCommand = cmd
Dim ds As New DataSetOP da.Fill(ds, "DataSetOP")

You need to change your data source. Since you using the the dataset as the datasource you already getting the filtered results.
You need to use the stored-procedure as your data source and then the parameters will appear

Related

How can I add a scalar variable for an SQL command In a function that doesn't house my query directly?

I will try to keep this as brief as possible.
I have a function called GetData(ByVal query As String) whose sole purpose is to populate a data table multiple times based on certain conditions. As you can see, the function accepts a string variable where the SQL statement resides. What I am trying to do is add a scalar variable, "#date" in my case, and no matter where I try to add this variable it throws an error stating "Must declare scalar variable #date.
Edit: I should mention that it is throwing the "must declare variable" error on the sda.Fill(dt) line.
GetData Function
Private Shared Function GetData(ByVal query As String) As DataTable
Dim constr As String = ConfigurationManager.ConnectionStrings("WarrantyConnectionString").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query)
Dim dt As DataTable = New DataTable()
cmd.Parameters.Add("#date", SqlDbType.Date).Value = Date.Today
Using sda As SqlDataAdapter = New SqlDataAdapter(query, con)
cmd.Parameters.AddWithValue("#date", Date.Today)
sda.Fill(dt)
End Using
Return dt
End Using
End Using
End Function
I am calling the function in a procedure that has the query and handles all of the conditions I need.
Procedure
Dim queryStart As String = "SELECT ( SELECT SUM(DealerNet) FROM Agreement WHERE VoidDate IS NULL "
Dim queryAlias As String = "AS Actual, "
Dim queryStart2 As String = "(SELECT SUM(Amount) FROM AccountingUS.dbo.ProjectedSales "
Dim queryAlias2 As String = "AS Projected "
If chart = "pmtd" Then
Dim queryCondition As String = "AND IssueDate BETWEEN (SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, #date)-1, 0)) AND #date) "
Dim queryCondition2 As String = "WHERE [Month] = MONTH(#date) AND [Year] = YEAR(#date)) "
Dim query As String = queryStart + queryCondition + queryAlias + queryStart2 + queryCondition2 + queryAlias2
Dim xMember1 As String = "Actual"
Dim xMember2 As String = "Projected"
Dim dt As DataTable = GetData(query)
pmtdChart.DataSource = dt
The variable in question is the #date variable in the strings within the "If" statement, the only value it holds is todays date. Currently, I have tried to use "cmd.Parameters.Add("#date", SqlDbType.Date).Value = Date.Today in the GetData function, however, I still receive the same "Must declare scalar variable" error. I have also tried replacing the #date variable with simply "" + Date.Today + "" or a variable that holds todays date, but upon doing so I receive an operand error about "Operand Clash: Date is incompatible with Int"
Any help regarding this issue would be greatly appreciated, I am relatively new to programming and would appreciate any tips or criticisms regarding best practices. If you need any additional information or clarification regarding this issue I would be happy to provide what I can. Thank you in advance.
Ok, a few things:
I would actually pass a command object to that get data routine.
And your issue is you feeding the query to the "adaptor", but NOT supplying the #date parameter to that "sda"
this:
Using sda As SqlDataAdapter = New SqlDataAdapter(query, con)
cmd.Parameters.AddWithValue("#date", Date.Today)
sda.Fill(dt)
End Using
In other words, you NOT EVEN using the cmd object!!!
So, you would need to add the parameter's to the sda object!!
eg this:
Public Function GetData(ByVal query As String) As DataTable
Dim dt As DataTable = New DataTable()
Dim constr As String =
ConfigurationManager.ConnectionStrings("WarrantyConnectionString").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter(query, con)
sda.SelectCommand.Parameters.Add("#date", SqlDbType.Date).Value = Date.Today()
sda.Fill(dt)
End Using
End Using
Return dt
End Function
So, yes, you WILL get that error about "#date" not being declared, since you NOT using the cmd object to fill the table, but are using the data adaptor.
So, as a future suggest?
Pick one way, or the other way.
I MUCH over the years have decided that I will use/have/adopt and cookie cut over and over the SqlCommand object.
I find the Sql cmd object better, since:
it has the parameters.
it has a connection object (if you want to use)
it has a data reader built in
So, what this means?
I suggest this code for get data:
Private Shared Function GetData(ByVal query As String) As DataTable
Dim constr As String =
ConfigurationManager.ConnectionStrings("WarrantyConnectionString").ConnectionString
Dim dt As DataTable = New DataTable()
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query, con))
con.Open()
cmd.Parameters.Add("#date", SqlDbType.Date).Value = Date.Today
dt.Load(cmd.ExecuteReader)
End Using
End Using
Return dt
End Function
So, we don't need a data adaptor. In fact, you only need a adaptor if you going to update the resulting table (think a "adaptive" table to remember this). You not going to update the data, so really, no need to use a "adaptor" at all here. (and sadly, far too many examples use a "adaptor" anyway. They are for ALLOWING update of the data table, and you not doing that!
So, use a command object. Do note that you ALWAYS must then open the confection, but since we have "using" blocks, it will ALWAYS be closed for you.
And note how then we don't create to "use" the "reader" from the adaptor, nor a fill command. (so, we eliminated one whole confusing object!!).
So, in your example, you created a SQL command object, correctly added the parameter to the command object, but THEN DON'T use it, and then decided to create a data adaptor, and use that!!!
So, you could/can leave your code as you had with the sda "prameter " fix I posted above.
However, but I think your better off to use a sql command object.
Note even better?
Pass the command object to the GetData routine.
I have a global "general" purpose routine called MyRstP(), and I pass it a command object, even for just plain jane sql.
but, if you decide to add parameter's, you can!
Do note that parameter's can be added 100% independent of the SQL string, and they can be added before, or after you set the sql string.
And you can add parameter's WITHOUT a valid working connection (or have created one just yet). So, "parameters" are just a colleciton - it does not care about the SQL (well, at least not yet!!).
So, here is my RstP, and I dumped this into a plain jane "module1" which VB has (this means you don't have to create a static class, and this works then just like VB6, or VBA.
So, this:
Public Function MyRstP(cmdSQL As SqlCommand, ByVal Optional strCon As String = "") As DataTable
If strCon = "" Then
strCon = My.Settings.TEST4
End If
Dim rstData As New DataTable
Using conn As New SqlConnection(strCon)
Using (cmdSQL)
cmdSQL.Connection = conn
conn.Open()
rstData.Load(cmdSQL.ExecuteReader)
End Using
End Using
Return rstData
End Function
So, now to say fill a grid view, I use this:
Dim strSQL As String =
"SELECT id, HotelName, City FROM tblHotelsA"
Dim cmdSQL As New SqlCommand(strSQL)
GridView1.DataSource = MyRstP(cmdSQL)
GridView1.DataBind()
or say a given date of some such:
How about all hotel visit dates from start of year.
So, this:
Dim strSQL As String =
"SELECT id, HotelName, City FROM tblHotelsA
WHERE VisitDate >= #dtStart"
Dim dtStart As DateTime
dtStart = DateSerial(DateTime.Today.Year, 1, 1)
Dim cmdSQL As New SqlCommand(strSQL)
cmdSQL.Parameters.Add("#dtStart", SqlDbType.DateTime).Value = dtStart
GridView1.DataSource = MyRstP(cmdSQL)
GridView1.DataBind()
note then how I have that MyRstP (like your get data), but I can pass it quite much anything I want, including parameter's from the "calling" code, NOT in that general routine.
Anyway, the above use and adding the parameter's to the "adaptor" will fix this, but I would change over to using just a command object and a connection - the adaptor really not required, and as noted, they really are to be used WHEN you actually want to update the data table, and then send it back to the database in one shot.
If you look closely, you setup a cmd command, but you never actually pass it to the DataTable. So it doesn't know anything about your params.
How about this instead (copied untested from Trying to pass SqlCommand in SqlDataAdapter as parameters):
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings("WarrantyConnectionString").ConnectionString))
{
con.Open();
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("#date", SqlDbType.Date)
cmd.Parameters.AddWithValue("#date", Date.Today)
using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
{
adp.Fill(dt);
return dt;
}
}
}
Dim dt as new DataTable()
using db as new SqlConnection(ConfigurationManager.ConnectionStrings("WarrantyConnectionString").ConnectionString)
db.Open();
using cmd as New SqlCommand(query, con)
cmd.Parameters.Add("#date", SqlDbType.Date).value = Date.Today
//cmd.Parameters.AddWithValue("#date", Date.Today)
using adp as new SqlDataAdapter(cmd)
adp.Fill(dt)
return dt
End using
End using
End using

crystal reports giving Database logon failed or asking for credentials

i have tried almost everything I could. I have a web app in ASP.net with c#. I am fetching data from the database tables and adding it to dataset. Then I set this dataset as the source to the report. My code is as following.
con.ConnectionString = ConfigurationManager.ConnectionStrings["familyConnectionString"].ConnectionString;
SqlDataAdapter sda = new SqlDataAdapter("select uid, member_name, gender, dob from family where uid='"+uid+"'", con);
DataSet1 myds = new DataSet1();
sda.Fill(myds, "family");
SqlDataAdapter sda1 = new SqlDataAdapter("select id from birth_certificates where p_id='"+uid+"'", con);
sda1.Fill(myds, "birth_certificates");
ReportDocument rpt = new ReportDocument();
rpt.Load(Server.MapPath("birth_certi_report.rpt"));
rpt.Refresh();
rpt.SetDataSource(myds);
rpt.SetDatabaseLogon("","",#".\sqlexpress","project2");
CrystalReportViewer1.ReportSource = rpt;
CrystalReportViewer1.DataBind();
CrystalReportViewer1.Visible = true;
CrystalReportViewer1.RefreshReport();
I am using integrated security so I left username and password blank. Please help.
Please Note that the above code is written inside a DropDown_selectedIndexChanged() event. I tried adding it to the page_load but it didn't work.
If you are not worried about authentication ,then do it this way.Create an instance of your crystal report and set its data source.Thats all you need to do and it works.
*Make sure the crystal report is within your solution.
con.ConnectionString = ConfigurationManager.ConnectionStrings["familyConnectionString"].ConnectionString;
// the magic
birth_certi_report myreport = new birth_certi_report();
DataSet1 myds = new DataSet1();
SqlDataAdapter sda1 = new SqlDataAdapter("select id from birth_certificates where p_id='"+uid+"'", con);
sda1.Fill(myds, "birth_certificates");
myreport.SetDataSource(myds);
crystalReportViewer1.ReportSource = myreport;
birth_certi_report myreport = new birth_certi_report();
in this line you have mentioned that birth_certi_report is crystal report. my crystal report name is applicationreport but it shows an error.
Dim myreport As New appilcationreport
error is TYPE IS NOT DEFINED

ASP.NET Grid View Issue

I am Newbie to NET I have been fighting with this issue from 3 days unable to solve ![1
The Data base names are Tbl_Employees,Tbl_Project
i have to fetch Username and Project two fields from the above two tables using project_id as primary key between them and load the data in grid view .Inputs needed for Loading the Grid
I'm am not sure what exactly you require but it seems like the basics of grid view. Please use this excellent reference to assist your problem. http://technico.qnownow.com/tag/gridview/
Because the tag was changed from c# to vb.net I'll leave my previous answer. Please try the following. I've added a SqlCommand
The VB.NET code
Dim da As New SqlDataAdapter
Dim DS As New DataSet
Dim mySelectQuery As String = "SELECT dbo.Tbl_Project.Project, dbo.Tbl_Employees.User_Name FROM dbo.Tbl_Employees INNER JOIN dbo.Tbl_Project ON dbo.Tbl_Employees.Project_ID = dbo.Tbl_Project.Project_ID"
Dim ConnString As String = "Data Source=yourSQLServer; Initial Catalog=yourDB; User Id=yourUserName; Password=yourPwd;" ''Change to you database/server specifics
Dim myConnection As New SqlConnection(ConnString)
Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
myConnection.Open()
da.SelectCommand = myCommand
da.Fill(DS,"Project")
If Not DS.Tables(0).Rows.Count > 0 Then
MessageBox.Show("There were no results found. ", "No Results Found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
gridview3.DataSource = Ds
gridview3.DataBind()
myConnection.Close()
End If
References to connection strings
http://www.sql-server-helper.com/sql-server-2008/sqlconnection-connection-string.aspx

Crystal Reports and strongly typed datasets yields empty report

I am converting an ASP.Net app from VS 2003 to VS 2005 as a starting point. The app uses Crystal Reports and binds using ADO.Net to a strongly typed dataset (XSD). I had to change some of Crystal Code to work with the newer version of Crystal. Now, when I run the page, the report generates, but none of the fields fill in. I have seen lots of people having the same problem with no real solutions out there. I decided to create a fresh project that does the same thing to remove the conversation from VS 2003 to 2005 as a possible cause of the problem. So my sample program has a button that runs a query, fills the dataset and assigns it to the report. The report displays the headers only. The code is below. I have no idea what to try next.
DataSet1 ds = new DataSet1();
SqlConnection conn =
new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("select * from mytable", conn);
da.Fill(ds);
ReportDocument rep = new ReportDocument();
rep.Load(Server.MapPath("crystalreport.rpt"));
rep.SetDataSource(ds);
CrystalReportViewer1.ReportSource = rep;
CrystalReportViewer1.RefreshReport();
I also created the DataSet1.XSD based on the same MYTABLE table. I get no errors or any indication anything is wrong except that the fields in the report don't populate.
It would take some debugging to know for sure why it's not working for you. Have you looked at the resulting dataset in a debugging session, and seen if it fills correctly?
Here's a good example of a method to work from.
SqlConnection cnn;
string connectionString = null;
string sql = null;
connectionString = "data source=SERVERNAME;initial catalog=DATABASENAME;user id=USERNAME;password=PASSWORD;";
cnn = new SqlConnection(connectionString);
cnn.Open();
sql = "select * from mytable";
SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
DataSet1 ds = new DataSet1();
dscmd.Fill(ds, "mytable");
cnn.Close();
CrystalReport1 objRpt = new CrystalReport1();
objRpt.SetDataSource(ds.Tables[1]);
crystalReportViewer1.ReportSource = objRpt;
crystalReportViewer1.Refresh();

SQL Server 2005

i created one ASP.Net project using SQL Server 2005 .I successfully inserted .But i dont know to view the table.Please tell me
A beginner instrudciton into SQL server seems in order - you seem to stumble around not knowing anything about hwat you really deal with.
I suggest heading over to ASP.NET (http://www.asp.net/) and read the indroduction documentation, as well as have some look at the sample code and the beginner forum.
Well this is a pretty broad question, but to get data into a datatable you could do something like:
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLConnection"].ConnectionString);
SqlCommand cmd = new SqlCommand("Retrieve", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#ID", SqlDbType.Int).Value = ID;
SqlDataAdapter sqldata = new SqlDataAdapter(cmd);
con.Open();
sqldata.Fill(dt);
con.Close();

Resources