Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-16-2005, 10:04 AM   #1 (permalink)
doobiwan
Registered User
 
doobiwan's Avatar
 
Join Date: May 2005
Posts: 2
doobiwan is on a distinguished road
help with SQL and VB.NET

hola,

starting to play with databasing and w dynamic web design, and for one of my first projects i'm coding a simple blog app.

i have two questions that i'm hoping someone here can help with.

A> first, to retrieve the info from the blog, i have 2 different statements for 2 different user controls. the first (on the front page) selects the top 3 (newest) entries. the second (on the main blog page) selects all entries. below are the SQL statements:

select 3 newest: SELECT TOP 3 blog_date,blog_title,blog_entry FROM gsol_blog ORDER BY blog_id DESC;

select all: SELECT * blog_date,blog_title,blog_entry FROM gsol_blog ORDER BY blog_id DESC;

the SELECT TOP 3 statement does not work, can anyone tell me why? below is the error that i get:

System.Data.Odbc.OdbcException: ERROR [42000] [MySQL][ODBC 3.51 Driver][mysqld-4.1.8-nt-max]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '3 blog_id AS blog_date,blog_title,blog_entry FROM gsol_blog ORDE at System.Data.Odbc.OdbcConnection.HandleError(Handle Ref hrHandle, SQL_HANDLE hType, RETCODE retcode) at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(C ommandBehavior behavior, String method) at System.Data.Odbc.OdbcCommand.ExecuteReader(Command Behavior behavior) at ASP.blogReadtop3_aspx.Page_Load() in C:\Sites\Single9\doobiwankenobi\webroot\blogreadto p3.aspx:line 23

i honestly don't know much about SQL yet, but this statement looks valid to me (unless my brain is fried, which is a definate possibility).



B> second, i have a simple page that i wrote to create entries and post them to the blog table in my mySQL db. the INSERT statement works, but i cannot put apostrophes or certain other punctuation into my blog entry .i'm assuming that because of the way i've coded it, these punctuation marks are being interpreted into the SQL statement and throwing the code. i would love it if someone could advise me on a better way to code my input page so that punctuation in my entries would not affect the SQL statement when the page is submitted. below is the page code:


<%@ Page Language="VB" Debug="True" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Odbc" %>
<script runat="server">

Sub Page_load()

Dim timeStamp as string = dateTime.now.toString()
lbldate.text = timestamp

End Sub

Sub blogSubmit(s as object, e as eventArgs)
Dim connStr, qry As String
connStr = "Driver={MySQL ODBC 3.51 Driver};Server=****;uid=****;pwd=****;Database=*** *;"
qry = "INSERT INTO gsol_blog(blog_date,blog_title,blog_entry) VALUES ('"+lblDate.text+"','"+entryTitle.text+"', '"+entryBody.text+"')"

Try
Dim conn As OdbcConnection
conn = new OdbcConnection(connStr)

conn.Open()

Dim cmd As OdbcCommand
cmd = new OdbcCommand(qry, conn)
cmd.executeNonQuery()
conn.Close()
conn = Nothing
cmd = Nothing
entryTitle.text = ""
entryBody.text = ""
response.redirect("index.htm")
Catch ex As Exception
Response.Write(ex)
End Try
End Sub

</script>
<html>
<head>
<title>blog entry - test</title>
</head>
<body>
<form runat="server">
<asp:label id="lblDate" runat="server" /><br />
<asp:textbox id="entryTitle" runat="server"
textmode="singleline"
columns="50" /><br />
<asp:textbox id="entryBody" runat="server"
textmode="multiline"
columns="50"
rows="25" /><br />
<asp:button text="submit" onClick="blogSubmit" runat="server" />
<asp:label id="lblResults" runat="server" />


</form>
</body>
</html>


like i said, it's a really simple page and it works...but i'm sure that there is a better way to put the text values into the INSERT statement so that i don't have to restrict myself to not using punctuation that will cause problems.

i thank anyone in advance that has the time and the willingness to throw me a bone. this has been annoying me greatly all weekend. i'm sure it's something really simple and obvious, too....but i just can't figure it out.

once again, thanks for any help on this.
doobiwan is offline   Reply With Quote
Old 06-10-2005, 01:35 AM   #2 (permalink)
kamaltherocky
Registered User
 
Join Date: Jun 2005
Posts: 3
kamaltherocky is on a distinguished road
MySQL Syntax

Hi

I think you are using MySQL as your DB.

To Limit the number of rows to 3, the Syntax in MySQL is

SELECT product, descr, email
FROM products
LIMIT 10

The one what you have written works in SQL Server.

Thanks
kamal
kamaltherocky is offline   Reply With Quote
Old 06-10-2005, 01:37 AM   #3 (permalink)
kamaltherocky
Registered User
 
Join Date: Jun 2005
Posts: 3
kamaltherocky is on a distinguished road
Single Quotes in Insert

Hi

For your Second Problem, before putting the data into the Insert statement, you need to replace single quotes and double quotes into DB specific escape characters. I am not sure what is the escape characters in MYSQL..but you can find out in the web.

Thanks
kamal
kamaltherocky is offline   Reply With Quote
Old 06-10-2005, 01:38 AM   #4 (permalink)
kamaltherocky
Registered User
 
Join Date: Jun 2005
Posts: 3
kamaltherocky is on a distinguished road
Correction

Sorry to Limit to 3 rows the SQL is

SELECT product, descr, email
FROM products
LIMIT 3

Regards
kamal
kamaltherocky is offline   Reply With Quote
Old 06-10-2005, 07:09 AM   #5 (permalink)
doobiwan
Registered User
 
doobiwan's Avatar
 
Join Date: May 2005
Posts: 2
doobiwan is on a distinguished road
thanks for the advice, i'll give it a try.
doobiwan is offline   Reply With Quote
Reply

Bookmarks

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 On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VB.Net and Crystal Reports matthew MS Technologies ( ASP, VB, C#, .NET ) 1 04-19-2005 12:19 PM
VB.Net unicorn1C MS Technologies ( ASP, VB, C#, .NET ) 1 04-07-2005 09:02 PM


All times are GMT -8. The time now is 02:13 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting