Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 6th, 2009, 03:39 PM
bex bex is offline
Friend of Wrox
 
Join Date: Aug 2008
Location: london, , United Kingdom.
Posts: 154
Thanks: 7
Thanked 1 Time in 1 Post
Default How to cover the SQLdatasource1 from designer to Hardcode

Hi
i am using a CustomValidator1 to see if the txtname.text exists in the database,
this is the code:
Code:
ProtectedSub CustomValidator1_ServerValidate(ByVal source AsObject, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
args.IsValid = True
ForEach drv As DataRowView In SqlDatasource1.Select(DataSourceSelectArguments.Empty)
If drv("Name").ToString() = args.Value Then
args.IsValid = False
ExitFor
EndIf
EndSub
the problem is that i created the data-source in design mode and it looks in 1 specific table, i want to make the data-source dynamic, something like this:

Code:
Dim  SqlDatasource1 As New  SqlConnection(ConfigurationManager.ConnectionStrings("test").ConnectionString)
Dim cmd AsNew SqlCommand("SELECT Campaign FROM " & Textbox1.text & "")
i tried to do it this way but i got an error saying SqlDatasource1 is not declared in the current document. is there a way i can do this?

thanks
__________________
bx
Reply With Quote
  #2 (permalink)  
Old March 6th, 2009, 04:19 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

You appear to be trying to create a SqlDataSource as a SqlConnection. That doesn't even make any sense.

You ought to be using Parameters for this anyway. Declare the variable in the SQL statement as a parameter, then feed the value of Textbox1 to it as a SelectParameter.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
Reply With Quote
  #3 (permalink)  
Old March 6th, 2009, 05:31 PM
bex bex is offline
Friend of Wrox
 
Join Date: Aug 2008
Location: london, , United Kingdom.
Posts: 154
Thanks: 7
Thanked 1 Time in 1 Post
Default

sory You are right That doesn't make any sense,

if i use this
Dim SqlDatasource1 As New SqlDataSource

how can i right the sql statement and associate it to the datasource?.
i know how to use parameters in this way:
Dim cmddd AsNew SqlCommand("select Name from Campaign Where Name= @Name")cmd.Parameters.Add("@Name", Data.SqlDbType.NVarChar, 50).Value = TextBox1.Text

in my case textbox1 is the name of the table i want to search, i don't need the
Where Name= @Name and don't know how to declare the table as parameter.

thank you .
__________________
bx
Reply With Quote
  #4 (permalink)  
Old March 6th, 2009, 05:50 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

I'm sorry, I must be thick as a brick today.

Just do I have this straight -- you have the column Campaign in more than one table, and you just want to get a list of campaigns that are in the rows of a given table? Even if there are duplicates?
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
Reply With Quote
  #5 (permalink)  
Old March 6th, 2009, 06:30 PM
bex bex is offline
Friend of Wrox
 
Join Date: Aug 2008
Location: london, , United Kingdom.
Posts: 154
Thanks: 7
Thanked 1 Time in 1 Post
Default

I have the column Name in more than one table, and want to look for names in the column Name of a given table.

this is all about to check the table for a name that is textbox2.text
if exist then
label1.text="name exist"
else
label1.text="name does not exist"

i am tying to do this so i can prevent duplicates
__________________
bx
Reply With Quote
  #6 (permalink)  
Old March 6th, 2009, 07:12 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Okay, I gotcha.

Well, in that case, you won't be able to use parameters. Sorry for that dumb suggestion.

But, you could simply manipulate the CommandText. You kinda had the right idea, but you need to do this on the server side, so that the server can determine what the value of TextBox1.Text and stick it in there for you.

Let's say you had this:

Code:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
          ConnectionString="<%$ ConnectionStrings:myConnectionString %>" 
          SelectCommand="SELECT [Campaign] FROM {0}" 
          OnSelecting="SqlDataSource1_Selecting">
</asp:SqlDataSource>
The {0} part will serve as your "wildcard". Then, in the Selecting event of this SqlDataSource, you would do this:

Code:
Protected Sub SqlDataSource1_Selecting(sender As Object, e As SqlDataSourceSelectingEventArgs)
    e.Command.CommandText = [String].Format(e.Command.CommandText, TextBox1.Text)
End Sub
This will substitute the value of TextBox1.Text for the {0} in your SelectCommand text string. Then, the selecting will proceed as normal from there.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
Reply With Quote
  #7 (permalink)  
Old March 7th, 2009, 02:31 PM
bex bex is offline
Friend of Wrox
 
Join Date: Aug 2008
Location: london, , United Kingdom.
Posts: 154
Thanks: 7
Thanked 1 Time in 1 Post
Default

thanks Lee ,

i tried the code and i got this error

i give the textbox1.text a default value to see if that is the problem still got the same error

Incorrect syntax near '0'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '0'.

Source Error:

Line 52: args.IsValid = TrueLine 53: Line 54: For Each drv As DataRowView In SqlDataSource1.Select(DataSourceSelectArguments.Em pty)Line 55: Line 56:
__________________
bx
Reply With Quote
  #8 (permalink)  
Old March 7th, 2009, 04:47 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,190
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

several things. Where did that for each comes from??

Also, DON'T YOU DARE TO DO SOMETHING LIKE THIS!

You are giving up an open SQL injection to the web! image I enter in your textbox this:

"Name; delete * FROM name;"

Then you are dommed. Since you are executing the sql statement, I just delete your whole table.

When you allow the user to enter text that you will use in query, you have to parametrized it. In your case, You will have to find another way to discover is the table exists, and then show the data to the user.
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
Reply With Quote
  #9 (permalink)  
Old March 7th, 2009, 05:15 PM
bex bex is offline
Friend of Wrox
 
Join Date: Aug 2008
Location: london, , United Kingdom.
Posts: 154
Thanks: 7
Thanked 1 Time in 1 Post
Default

I understand your concern,

first things first.
ProtectedSub CustomValidator1_ServerValidate(ByVal source AsObject, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
args.IsValid = True
ForEach drv As DataRowView In SqlDatasource1.Select(DataSourceSelectArguments.Em pty)
If drv("Name").ToString() = args.Value Then
args.IsValid = False
ExitFor
EndIf
EndSub

is the code that checks if name exists, i used
CustomValidator to search the table


second
the textbox is read only and is databind, so if a user logs in he/she is associated to a campaign, that campaign name will appear in the textbox, no one can edit the textbox.
lets say i have 2 users and 2 campaigns,(User1, User2) Campaign1, Campaign2

User1 is associated with Campaign1, User2 associated Campaign2.
if user1 logs in i want User1 to search Campaign1

User2 search Campaign2
this is why i need the textbox1, i might have taken the wrong approach but this is all i can thing off.

Campaign1 /Campaign2 are 2 tables
__________________
bx
Reply With Quote
  #10 (permalink)  
Old March 7th, 2009, 05:30 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,190
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Yes. The problem is use a different table for every campaign. What if tomorrow someone tell you: "Now we have 50 campaings". Will you add 48 more tables???

You need only one table, with a column that holds the number of campaign. Add a column also to the user table that tells you which campaign he is involved. In fact, if a user can have one or more campaigns, then you better has that info in a separate table.

Also the error is because the query doesn't have any parameter!
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Web Designer to UI Designer jitesh82 BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 2 April 16th, 2011 02:04 AM
how can i cover all datagrid rows ??? thomaz C# 1 November 30th, 2006 10:19 PM
Back Cover Error mitmaro BOOK: Ivor Horton's Beginning Visual C++ 2005 0 July 9th, 2006 03:22 AM
how can i cover all itens of a list view? thomaz C# 1 November 30th, 2005 04:30 AM
Book cover and ISBN Misleading.. kitjunk Wrox Book Feedback 1 June 17th, 2003 01:27 PM



All times are GMT -4. The time now is 10:14 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.