Solved

VB.NET to C#

Posted on 2006-06-23
10
433 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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
 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

737 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