Wednesday, March 7, 2012
Programmatically change a textbox's contents
control on it. In the code-behind I set the datasource of the report and
then view the report data:
DataSet ds = DBProvider.GetDataSet(SQL, DateFrom, DateTo);
rvRR310.LocalReport.ReportPath = "RR310.rdlc";
rvRR310.LocalReport.DataSources.Clear();
rvRR310.LocalReport.DataSources.Add(new
ReportDataSource("Reports_stp_RR310_Export", ds.Tables[0]));
"DBProvider" is a class I made to talk to the SQL Server and "RR310.rdlc" is
the actual report itself. The query that is executed accepts a couple
parameters (start date and end date) for the data that is to be returned.
My issue is not with this part, as everything up to this point works great.
What I am trying to figure out how to do is access the report itself, so I
can change the contents of a textbox I put there. I want to take the query
parameters and stuff them into the report textbox so I can have it say
something like "Date Range: <start> to <end>". The problem is I can't
figure out how to write the code to allow me to do this.
Anyone have a suggestion or pointer? Thanks!
-- AndrewOn May 23, 5:10 pm, "Andrew" <andrewr2k1@.n0r3ply_hotmail.com> wrote:
> Quick question... I have a C# ASP.Net (2.0) webpage with a ReportViewer
> control on it. In the code-behind I set the datasource of the report and
> then view the report data:
> DataSet ds = DBProvider.GetDataSet(SQL, DateFrom, DateTo);
> rvRR310.LocalReport.ReportPath = "RR310.rdlc";
> rvRR310.LocalReport.DataSources.Clear();
> rvRR310.LocalReport.DataSources.Add(new
> ReportDataSource("Reports_stp_RR310_Export", ds.Tables[0]));
> "DBProvider" is a class I made to talk to the SQL Server and "RR310.rdlc" is
> the actual report itself. The query that is executed accepts a couple
> parameters (start date and end date) for the data that is to be returned.
> My issue is not with this part, as everything up to this point works great.
> What I am trying to figure out how to do is access the report itself, so I
> can change the contents of a textbox I put there. I want to take the query
> parameters and stuff them into the report textbox so I can have it say
> something like "Date Range: <start> to <end>". The problem is I can't
> figure out how to write the code to allow me to do this.
> Anyone have a suggestion or pointer? Thanks!
> -- Andrew
If I understand you correctly, you should be able to achieve this by
referencing the report parameters. Something like this should work as
expressions in the textbox controls in the report itself.
=Parameters!StartDate.Value
=Parameters!EndDate.Value
Regards,
Enrique Martinez
Sr. Software Consultant|||I would agree with you if the report knew about the parameters, but the
report never sees them. As shown in the code snippet, I pass the SQL stored
procedure, and the arguments, to a method of my class that talks to the SQL
Server. It passes back a dataset which I then assign to the report. The
report only knows about the data, not any parameters.
Let me ask the question another way...
From the .cs page that has the ReportViewer control, how can I change the
text of a TextBox inside the report. Never mind where or what the content
is coming from, just, how do I change the text from outside the report
itself and at runtime?
"EMartinez" <emartinez.pr1@.gmail.com> wrote in message
news:1179970516.364360.86270@.m36g2000hse.googlegroups.com...
> On May 23, 5:10 pm, "Andrew" <andrewr2k1@.n0r3ply_hotmail.com> wrote:
>
> If I understand you correctly, you should be able to achieve this by
> referencing the report parameters. Something like this should work as
> expressions in the textbox controls in the report itself.
> =Parameters!StartDate.Value
> =Parameters!EndDate.Value
> Regards,
> Enrique Martinez
> Sr. Software Consultant
>|||I found the answer to my question....
First step is to set up a Report Parameter (as many as you need for whatever
it is you need to do).
Second, add a TextBox to the report, and then under Expressions, set it to
display the Parameter you desire.
Last, in the .cs page that loads the ReportViewer control, add the following
code:
ReportParameter[] params = new ReportParameter[2];
params[0] = new ReportParameter("DateFrom", "ProductShipped", false);
params[1] = new ReportParameter("DateTo", "@.parameter1", false);
this.reportViewer.LocalReport.SetParameters(params);
Of course, change the names and size to suit your needs.
Pretty simple and straightforward, and works very nicely with what I wanted
to do.
-- Andrew
"Andrew" <andrewr2k1@.n0r3ply_hotmail.com> wrote in message
news:e413UcYnHHA.4476@.TK2MSFTNGP02.phx.gbl...
> Quick question... I have a C# ASP.Net (2.0) webpage with a ReportViewer
> control on it. In the code-behind I set the datasource of the report and
> then view the report data:
> DataSet ds = DBProvider.GetDataSet(SQL, DateFrom, DateTo);
> rvRR310.LocalReport.ReportPath = "RR310.rdlc";
> rvRR310.LocalReport.DataSources.Clear();
> rvRR310.LocalReport.DataSources.Add(new
> ReportDataSource("Reports_stp_RR310_Export", ds.Tables[0]));
> "DBProvider" is a class I made to talk to the SQL Server and "RR310.rdlc"
> is the actual report itself. The query that is executed accepts a couple
> parameters (start date and end date) for the data that is to be returned.
> My issue is not with this part, as everything up to this point works
> great. What I am trying to figure out how to do is access the report
> itself, so I can change the contents of a textbox I put there. I want to
> take the query parameters and stuff them into the report textbox so I can
> have it say something like "Date Range: <start> to <end>". The problem is
> I can't figure out how to write the code to allow me to do this.
> Anyone have a suggestion or pointer? Thanks!
> -- Andrew
>
Saturday, February 25, 2012
Programmatic insert into SQL database
I have a page with over 20 Textbox and DDL controls and an upload in various Divs and Panels (Ajax enabled) that are used for gathering user data. Some of the fields are mandatory and some optional.
In the Code behind (VB- I am a complete novice) On the submit button click event, I iterate through the controls in the page and build an array with information from the controls that have data in them, (filtering out the non-filled textboxes, and DDLs).
All this works well, and I get an array called 'myInfo' with the columns with the control ID, and control values 'rvalue' (as string), with the number of rows equal to the filled textboxes and DDLs.
I then step through the array and build a string with 'name=values' of all the rows in the myinfo array and email this as a message:
ThisMessage = ""
NoOfControls = myInfo.GetLength(0)
For i = 0 To NoOfControls - 1
ThisMessage = ThisMessage & myInfo(i).ID.ToString & "=" & myInfo(i).rvalue.ToString & "; "
Next
SendMail(email address, ThisMessage)
I also want to add this information to a database, appended by the IP address and datetime.now.
Dim evdoDataSource As New SqlDataSource()
evdoDataSource.ConnectionString = ConfigurationManager.ConnectionStrings("SQLConnectionStringCK").ToString
evdoDataSource.InsertCommandType = SqlDataSourceCommandType.Text
Dim InsertMessage As String = """INSERT INTO evdoData ("
NoOfControls = myInfo.GetLength(0)
Dim k As Integer
For k = 0 To NoOfControls - 1
InsertMessage = InsertMessage & myInfo(k).ID.ToString & ", "
Next
InsertMessage = InsertMessage & "IPNo, DateEntered) VALUES ("
For k = 0 To NoOfControls - 1
InsertMessage = InsertMessage & "@." & myInfo(k).ID.ToString & ", "
Next
InsertMessage = InsertMessage & ", @.IPNo, @.DateEntered" & ")"""
evdoDataSource.InsertCommand = InsertMessage
I then similarly iterate through and do the insertparameters.
Now here is the rub- (My all too often DUH moment!)
When I look at the insertmessage in debug (and to be sure- I also show the insert string on a temporary debug label on the page), The insertmessage looks fine:
"INSERT INTO EvdData (FirstName, Age, Email, Phone, Country, City, IPNo, DateEntered) VALUES (@.FirstName, @.Age, @.Email, @.Phone, @.Country, @.City, , @.IPNo, @.DateEntered)"
However when the above code(evdoDataSource.InsertCommand = InsertMessage) is run, I get an error - the message with the error is:
The identifier that starts with 'INSERT INTO ModelData (FirstName, Age, Email, Phone, Country, City, IPNo, DateEntered) VALUES (@.FirsteName, @.Age, @.Email, @.Phone,' is too long. Maximum length is 128
EH? When I actually copy the InsertMessage from the debug window and paste it manually after the command "evdoDataSource.InsertCommand = " It works, and I get the data inserted into the table..
-It would seem that I am probably missing something obvious in my complete "noviceness" . HELP! (oh and thanks a bunch in anticipation)
_____________________________________________________________________________________________________________________
Its Easy --------When you know How. Meanwhile Aaaaaaaaah .
Remove the double-quotes around the insert statement.
Dim InsertMessageAs String ="INSERT INTO evdoData (" NoOfControls = myInfo.GetLength(0)Dim kAs Integer For k = 0To NoOfControls - 1 InsertMessage = InsertMessage & myInfo(k).ID.ToString &", "Next InsertMessage = InsertMessage &"IPNo, DateEntered) VALUES ("For k = 0To NoOfControls - 1 InsertMessage = InsertMessage &"@." & myInfo(k).ID.ToString &", "Next InsertMessage = InsertMessage &", @.IPNo, @.DateEntered)" |||
In the post the you have an extra comma in your insert statement after @.City and before @.IPNo
INSERT INTO EvdData (FirstName, Age, Email, Phone,Country, City, IPNo, DateEntered) VALUES (@.FirstName, @.Age, @.Email,@.Phone, @.Country, @.City, , @.IPNo, @.DateEntered)
You may want to change
InsertMessage = InsertMessage & ", @.IPNo, @.DateEntered" & ")"""
to
InsertMessage = InsertMessage & "@.IPNo, @.DateEntered" & ")"""
First, don't use " (double quotes) in your query, instead use ' (single quote) if and when needed. If at all you have to use double quotes then prefer setting QUOTED_IDENTIFIER OFF for the connection object. This sounds bit tricky, right?
kj@.zqtech.com:
"INSERT INTO EvdData (FirstName, Age, Email, Phone, Country, City, IPNo, DateEntered) VALUES (@.FirstName, @.Age, @.Email, @.Phone, @.Country, @.City, , @.IPNo, @.DateEntered)"
Second, did you notice that there are 2 , (comma) between @.city and @.IPNO or this is just by mistake when you were pasting your code in the post ?
Hope this will help.
|||Thanks Guys. It was the combination of both things. No quotations required and also the extra comma. (Silly look on my face at this point).
Moral of the story:
1. Coding problems are like magician's tricks: one problem is a slight of hand to take your attention away from another. 2. Beware of taking error messages too literally. -