Solved

VB.NET to C#

Posted on 2006-06-23
10
421 Views
Last Modified: 2012-05-05
Hello Experts,

I am just trying to move a project from VB.NET to C# (With very little knowledge of C#)

Could anyone pls answer the following questions for me:

1. In my web.config I have:

<appSettings>
      <!--Utilities Keys-->
      <add key="NAGTYUtilities.ConnectionString" value="server=xxxxxx; uid=nitinsharma; pwd=$onityGenius1; database=QueryDev"/>            
       </appSettings>
</configuration>

How do I access it in other files to get the connectionstring, at present I am doing like shown below but that gives me error:
                     SqlConnection sqlconn = new SqlConnection();
                sqlconn.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings(1);

2. How Do I write the following VB Line into C# (I get very confused with "" and '')

 Booking.Attributes.Add("onclick", "javascript:window.open('BookingPage.aspx?@cid=" + Trim(DropDownList1.SelectedValue) + "&@ctext=" + Trim(DropDownList1.SelectedItem.Text) + "&@dday=" + Trim(Calendar1.SelectedDate) + "&@id=0" & "','_blank','status=yes,toolbar=no,width=550,height=370');")

Any help would b much appreciated.
Ave a gud day!
0
Comment
Question by:radhakrishan
  • 5
  • 3
  • 2
10 Comments
 
LVL 15

Accepted Solution

by:
GavinMannion earned 250 total points
Comment Utility
sqlconn.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings("NAGTYUtilities.ConnectionString");

Should work .... if nto what error do you get...

I cannot see anythign wrong with your second line of code except you are missing the ending semicolon ;

what I see it :), after @id=0" you have an ampersand & for no reason, take away the following " & "

It should work then
0
 
LVL 3

Assisted Solution

by:harshits
harshits earned 100 total points
Comment Utility
Hi,

Here is answer to your first question. The code for fetching a appsetting value from web.config is

 SqlConnection sqlconn = new SqlConnection();
sqlconn.ConnectionString =System.Configuration.ConfigurationSettings.AppSettings["NAGTYUtilities.ConnectionString"].ToString();

I was not able to get your second query. If possible could you provide me the original piece of vb.net code which you are trying to convert to c#. May be then It might be a little more clear.

Harshit Sheth

0
 

Author Comment

by:radhakrishan
Comment Utility

sqlconn.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings("NAGTYUtilities.ConnectionString");
Error I recieve while doing above is:
'System.Configuration.ConfigurationSettings.AppSettings' denotes a 'property' where a 'method' was expected


Secondly, Even removing the & from it would look like:

 Booking.Attributes.Add("onclick", "javascript:window.open('BookingPage.aspx?@cid=" + Trim(DropDownList1.SelectedValue) + "&@ctext=" + Trim(DropDownList1.SelectedItem.Text) + "&@dday=" + Trim(Calendar1.SelectedDate) + "&@id=0"','_blank','status=yes,toolbar=no,width=550,height=370');")

It comes with loads of errors of expecting ";" ....

0
 
LVL 3

Expert Comment

by:harshits
Comment Utility
Hey radhakrishan ,

"( )" won't work in c# you'll have to use "[ ]" brackets. That is the reason you are getting the error.

Harshit Sheth
0
 

Author Comment

by:radhakrishan
Comment Utility
Hi Harshit,

So should be like this:


 Booking.Attributes.Add["onclick", "javascript:window.open['BookingPage.aspx?@cid=" + Trim[DropDownList1.SelectedValue] + "&@ctext=" + Trim[DropDownList1.SelectedItem.Text] + "&@dday=" + Trim[Calendar1.SelectedDate] + "&@id=0"','_blank','status=yes,toolbar=no,width=550,height=370'];"]

That got errors of its own...


My Exact VB Code is:
 Booking.Attributes.Add("onclick", "javascript:window.open('BookingPage.aspx?@cid=" + Trim(DropDownList1.SelectedValue) + "&@ctext=" + Trim(DropDownList1.SelectedItem.Text) + "&@dday=" + Trim(Calendar1.SelectedDate) + "&@id=0" & "','_blank','status=yes,toolbar=no,width=550,height=370');")

which needs change to C#.


Thanks.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:radhakrishan
Comment Utility
I think the problem is with the Querystring input bcoz if I do
this.Booking.Attributes.Add("onclick","javascript:window.open('BookingPage.aspx' ,'_blank','status=yes,toolbar=no,width=550,height=370')");

that works fine.

But I want 4 querystring to go with it:

@cid = DropdownList.SelectedValue.Trim()
@ctext = Dropdownlist.SelectedItem.Text.Trim()
@day = Calendar1.SelectedDate
@id = 0

If someone could tell me how to send those query strings that would b great!

0
 
LVL 15

Expert Comment

by:GavinMannion
Comment Utility
Firstly as harshits said, I left the ( brackets around the connection string instead of putting the square ones...

Make it
sqlconn.ConnectionString =System.Configuration.ConfigurationSettings.AppSettings["NAGTYUtilities.ConnectionString"].ToString();

On the second part after you took away the ampersand you forgot to put the semicolon right at the end.

ALL C# statements must end with a semi colon
0
 

Author Comment

by:radhakrishan
Comment Utility
Hi,

AS you mentioned doing this
 this.Booking.Attributes.Add("onclick", "javascript:window.open('BookingPage.aspx?@cid=" + Trim(DropDownList1.SelectedValue) + "&@ctext=" + Trim(DropDownList1.SelectedItem.Text) + "&@dday=" + Trim(Calendar1.SelectedDate) + "&@id=0"','_blank','status=yes,toolbar=no,width=550,height=370');");

Brings up 18 errors of either ";" Expected or "," invalid character.......

If I remove all the querystrings this seems to work fine for me, but i need all those.

Could you pls write the same statement with the querystrings i mentioned above and i could check if it really matches, bcoz surely the prob is in there!

0
 
LVL 15

Expert Comment

by:GavinMannion
Comment Utility
Unfortunately I don't have a dev environment on hand to test this in otherwise I would....

Okay looking again it looks like you have an extra " after @id=0

Take that away..

You should only have "'s before javascript... just before the end closing brackets and then on each side of + something +
0
 

Author Comment

by:radhakrishan
Comment Utility
Hi,


It works fine, avent tested but error free atleast!

(I have split the pts bcoz of both of u guys help!)

Cheers!!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now