Solved

VB.NET to C#

Posted on 2006-06-23
10
425 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
ID: 16967662
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
ID: 16967708
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
ID: 16967726

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
ID: 16967786
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
ID: 16967842
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:radhakrishan
ID: 16967940
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
ID: 16968052
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
ID: 16968116
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
ID: 16968423
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
ID: 16968477
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
javascript window.createPopup() not working in chrome but works in IE browser 3 96
Asp.net mvc 5 5 42
Syntax error 9 45
Replace &lt; with < 14 53
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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 is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

947 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

22 Experts available now in Live!

Get 1:1 Help Now