Welcome to Dream.In.Code
Become an Expert!

Join 150,064 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,775 people online right now. Registration is fast and FREE... Join Now!




Modifying an SQL query with user input

 
Reply to this topicStart new topic

Modifying an SQL query with user input

Fullchaos
12 Jun, 2008 - 08:40 AM
Post #1

New D.I.C Head
*

Joined: 30 May, 2008
Posts: 30


My Contributions
So long story short, after some help getting connected to mysql, I took some time off to ready the data for entry.

After coming back, the objective hasn't changed. Essentially the goal is to take a string from a user input, and feed it into the SQL string and get back information.

The database will return the table and the grid when the statement is:
CODE
"SELECT family,model_number,operation,description,documentation,tooling FROM 889dmaster "


The string input works as well, as it produces a result upon the button being clicked.

So essentially: Asp.net ------*connected*------ Mysql
User Input ----*connected* ----- string
SQL Statement ------- Mysql ---*returns table*
String -------- --------- SQL statement --------- Mysql

Here's the full code:
CODE
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Common" %>
<%@ Import Namespace="MySql.Data" %>
<%@ Import Namespace="MySql.Data.MySqlClient" %>

<script language = "VB" runat="server" >
    Dim whereval As String
    
    Sub buttonclick1(ByVal sender As Object, ByVal e As System.EventArgs)
        whereval = InputBox("Please enter your model number as it appears.  Replace the footage of your model number with a *", "Model Number Entry")
        
        
    End Sub
        
</script>

<script language = "VB" runat = "server">
    Private Const connectString As String = "Server=localhost;Database=a;User=root;Password=mp;"
  
   Function GetDataAdapter(ByVal sql As String) As DbDataAdapter
      Return New MySqlDataAdapter(sql, connectString)
   End Function
  
   Sub UpdateDataGrid()
      Dim myDataAdapter As DbDataAdapter
      Dim myDataSet As New DataSet

        myDataAdapter = GetDataAdapter("SELECT family,model_number,operation,description,documentation,tooling FROM 889dmaster " & _
                                       "WHERE model_number='" & whereval & "'")
        myDataAdapter.Fill(myDataSet, "889dmaster")
      
      ' check what we really got
        ' Label1.Text = myDataSet.GetXml()
      ' you may need to bind the table
      If myDataSet.Tables.Count = 1 Then
         MySQLDataGrid.DataSource = myDataSet.Tables(0)
         MySQLDataGrid.DataBind()
      End If
      
   End Sub
  
    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        MyBase.OnLoad(e)
        ' Label1.Text = "Hit"
        UpdateDataGrid()
    End Sub
</script>



<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Database Query</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
  
  
   <%-- <script  runat="server">
       Sub submit(ByVal sender As Object, ByVal e As EventArgs)
            lbl1.Text = "Your model number is " & txt1.Text
       End Sub
    </script>--%>

    

       <asp:Button ID="Button2" OnClick="buttonclick1" Text="Enter Model Number" runat="server" />
    <% Response.Write(whereval)%>
    
    
    <%--Enter model number:
    <asp:TextBox id="txt1" runat="server" />
    <asp:Button ID="Button1" OnClick="submit" Text="Submit" runat="server" />
    <p><asp:Label id="lbl1" runat="server" /></p>--%>
    </form>
    <asp:DataGrid ID ="MySQLDataGrid" runat="Server" >
    </asp:DataGrid>
    

    
    </div>
    </form>
</body>
</html>
(P.S. Thanks again baavgai)

So what I'm thinking is my problem lies with the WHERE statement since the datagrid still outputs the column names. It does not however output any of the values for them in the database (when a correct model is entered).

Any idea's what could be wrong, or how I can fix it?
User is offlineProfile CardPM
+Quote Post

Fullchaos
RE: Modifying An SQL Query With User Input
16 Jun, 2008 - 03:49 AM
Post #2

New D.I.C Head
*

Joined: 30 May, 2008
Posts: 30


My Contributions
So, I checked the string and it appears to be correct.

Could it be something with the datagrid?

Anybody have an idea?


User is offlineProfile CardPM
+Quote Post

Jayman
RE: Modifying An SQL Query With User Input
16 Jun, 2008 - 08:24 AM
Post #3

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
What data type is "model_number" in your database?

If the column names are being created correctly, then the problem is most likely that you are not getting any rows returned from your select statement.

Try running the SELECT statement directly inside a query editor for your database. If it returns results then post back a response and we can look further into your code for the problem.
User is offlineProfile CardPM
+Quote Post

Fullchaos
RE: Modifying An SQL Query With User Input
17 Jun, 2008 - 03:40 AM
Post #4

New D.I.C Head
*

Joined: 30 May, 2008
Posts: 30


My Contributions
Using the mysql query browser it produces the correct results. It also produces the correct results through the command line client.

The fields are made up of 6 different columns (varchar ranging from 10 to 255).

None of them are a primary key. Defaults are null. No extra information.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:47PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month