Solved

asp.net c# string and session

Posted on 2011-03-15
6
367 Views
Last Modified: 2012-06-21
     Session["UserId"] = PortalGlobals.UserId ;
        //string UserId = PortalGlobals.UserId;

For some reasons, if I tried to use //string UserId = PortalGlobals.UserId;
and the following sql. It will fail me....

Any experts have idea how to fix it?


   SqlDataSource1.SelectCommand = "SELECT CrmSchedule.*, CrmSchedule.MeetingCity + ', ' + CrmSchedule.MeetingState as MeetingCityAndState, CrmLeadSource.Description AS DescLeadSource, "
        + " CrmMeetingType.Description AS DescMeetingType, CrmScheduleStatus.Description AS DescScheduleStatus, "
        + " Customers.Name As CustName, Customers.MC_Number FROM CrmSchedule INNER JOIN"
        + " CrmLeadSource ON CrmSchedule.LeadSourceId = CrmLeadSource.LeadSourceId INNER JOIN"
        + " CrmMeetingType ON CrmSchedule.MeetingTypeId = CrmMeetingType.MeetingTypeId INNER JOIN"
        + " CrmScheduleStatus ON CrmSchedule.ScheduleStatusId = CrmScheduleStatus.ScheduleStatusId INNER JOIN"
        + " Customers ON CrmSchedule.CustomerId = Customers.ID Where CrmSchedule.UserId='" + UserId.ToString() + "'"
        + " Order By CrmSchedule.BeginDate Desc";      
   
protected void Page_Load(object sender, EventArgs e)
    {
        Session["UserId"] = PortalGlobals.UserId ;
        //string UserId = PortalGlobals.UserId;

        SqlDataSource1.SelectCommand = "SELECT CrmSchedule.*, CrmSchedule.MeetingCity + ', ' + CrmSchedule.MeetingState as MeetingCityAndState, CrmLeadSource.Description AS DescLeadSource, "
        + " CrmMeetingType.Description AS DescMeetingType, CrmScheduleStatus.Description AS DescScheduleStatus, "
        + " Customers.Name As CustName, Customers.MC_Number FROM CrmSchedule INNER JOIN"
        + " CrmLeadSource ON CrmSchedule.LeadSourceId = CrmLeadSource.LeadSourceId INNER JOIN"
        + " CrmMeetingType ON CrmSchedule.MeetingTypeId = CrmMeetingType.MeetingTypeId INNER JOIN"
        + " CrmScheduleStatus ON CrmSchedule.ScheduleStatusId = CrmScheduleStatus.ScheduleStatusId INNER JOIN"
        + " Customers ON CrmSchedule.CustomerId = Customers.ID Where CrmSchedule.UserId='" + Session["UserId"] + "'"
        + " Order By CrmSchedule.BeginDate Desc";       
    }

Open in new window

0
Comment
Question by:Webboy2008
  • 3
  • 3
6 Comments
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35141250
Do you mean if you uncomment that line and and change line 12 to + " Customers ON CrmSchedule.CustomerId = Customers.ID Where CrmSchedule.UserId='" + UserId + "'"?

How does it fail? What happens, or what exception do you get?
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35141343
Also, I think that's not quite a well-formed SQL command, line 6 is going to result in:

SELECT CrmSchedule.*, CrmSchedule.MeetingCity + ', ' + CrmSchedule.MeetingState as MeetingCityAndState, CrmLeadSource.Description AS DescLeadSource

Being sent to the SQL server, and those plus signs, quotes and comma aren't gonna make it happy - looks like you might have had some other variable in there, I would recommend writing your SQL statement as:

string commandText = @"SELECT CrmSchedule.*, CrmSchedule.MeetingCity, CrmSchedule.MeetingState as MeetingCityAndState, CrmLeadSource.Description AS DescLeadSource,
CrmMeetingType.Description AS DescMeetingType, CrmScheduleStatus.Description AS DescScheduleStatus,
Customers.Name As CustName, Customers.MC_Number FROM CrmSchedule INNER JOIN
CrmLeadSource ON CrmSchedule.LeadSourceId = CrmLeadSource.LeadSourceId INNER JOIN
CrmMeetingType ON CrmSchedule.MeetingTypeId = CrmMeetingType.MeetingTypeId INNER JOIN
CrmScheduleStatus ON CrmSchedule.ScheduleStatusId = CrmScheduleStatus.ScheduleStatusId INNER JOIN
Customers ON CrmSchedule.CustomerId = Customers.ID Where CrmSchedule.UserId='{0}'
Order By CrmSchedule.BeginDate Desc";

SqlDataSource1.SelectCommand = String.Format(commandText, PortalGlobals.UserID);

Open in new window


Or better yet, use SqlParameters (just not sure how to do that off the top of my head with the SqlDataSource, sorry).
0
 

Author Comment

by:Webboy2008
ID: 35143072
Error:

Compiler Error Message: CS0103: The name 'commandText' does not exist in the current context

Source Error:

 

Line 24:        + " Customers ON salesreps.CUSTOMER_ID = Customers.ID"
Line 25:          + " WHERE salesreps.USER_ID='{0}'";
Line 26:        SqlDataSource1.SelectCommand = String.Format(commandText,PortalGlobals.UserId);
Line 27:        
Line 28:
 
0
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

 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 35143115
Sorry I, I meant assigning to the SQL string to a string variable named "commandText" first, and then writing SqlDataSource1.SelectCommand = String.Format(commandText, PortalGlobals.UserId).  So you're Page_Load would look like:

protected void Page_Load(object sender, EventArgs e)
{
    // Putting the @ symbol in front of the string lets you write it over
    // several lines without needing to + together a bunch of different strings
    // (it also disables escape sequences like \r\n)
    string commandText = @"SELECT CrmSchedule.*, CrmSchedule.MeetingCity, CrmSchedule.MeetingState as MeetingCityAndState, CrmLeadSource.Description AS DescLeadSource,
CrmMeetingType.Description AS DescMeetingType, CrmScheduleStatus.Description AS DescScheduleStatus,
Customers.Name As CustName, Customers.MC_Number FROM CrmSchedule INNER JOIN
CrmLeadSource ON CrmSchedule.LeadSourceId = CrmLeadSource.LeadSourceId INNER JOIN
CrmMeetingType ON CrmSchedule.MeetingTypeId = CrmMeetingType.MeetingTypeId INNER JOIN
CrmScheduleStatus ON CrmSchedule.ScheduleStatusId = CrmScheduleStatus.ScheduleStatusId INNER JOIN
Customers ON CrmSchedule.CustomerId = Customers.ID Where CrmSchedule.UserId='{0}'
Order By CrmSchedule.BeginDate Desc";

    // The String.Format method will replace the {0} in commandText with the value
    // of PortalGlobals.UserId
    SqlDataSource1.SelectCommand = String.Format(commandText, PortalGlobals.UserId);
}

Open in new window


By the way, you need to be careful that PortalGlobals.UserId doesn't contain any special SQL characters, like the single quote (') in particular.  If this value comes from the user it's possible someone could take advantage of it and inject their own SQL statement - one reason why using SqlParameters is a good idea.
0
 

Author Comment

by:Webboy2008
ID: 35143289
Got it. Your point is coming.

Have another question if you interest to help:

http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_26889448.html
0
 

Author Closing Comment

by:Webboy2008
ID: 35143291
Excellent Helps
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

11 Experts available now in Live!

Get 1:1 Help Now