Nevron Diagram
Skip Navigation Links
Home
Forum Home
Latest 50
Unanswered
Win Prizes
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName
   Home » ADO.NET & Database » Use Variable in query
       
Author Reply
David Carter
posted 31 posts
since Jul 22, 2010 
from Australia

Use Variable in query

  Posted on: 24 Aug 2010       

I have an OleDbAdapter attached to a datagridview.
I wish to take the value in a textbox to use in a query to fill the datagridview.
Eg. The following works: David Carter in this instance is typed into the Query as a constant.
OleDbDataAdapter1.SelectCommand.CommandText =
"Select * From InfringementRecords Where([INFStudent] = 'David Carter')"
But I don't want David Carter to be fixed in the query, I want to use a variable as entered into a textbox
Dim SelectName as string
SelectName = Textbox1.text
OleDbDataAdapter1.SelectCommand.CommandText =
"Select * From InfringementRecords Where([INFStudent] = " & SelectName & ")"
That gives me an error:
Syntax error (missing operator) in query expression '([INFStudent] = David Carter)'.
Does anyone know how to use a variable in a query?
Visual Studio 2008.
Regards,
David
 
Carter's Law of Productivity. "The amount of work a person does is inversely proportional to the number of suggestions they offer." - David J Carter
Satyapriya Nayak
posted  2231 posts
since  Mar 24, 2010 
from 

 Re: Use Variable in query
  Posted on: 24 Aug 2010        0  

Hi David,
This is a simple application i did it of your requirement.Run the attachments.
Code:-
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Display_data_in_datagridview_with_textbox
{
    public partial class Form1 : Form
    {
        string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
        OleDbCommand com;
        OleDbDataAdapter oledbda;
        DataSet ds;
        string str;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            bindgrid();
        }
        void bindgrid()
        {
            OleDbConnection con = new OleDbConnection(ConnectionString);
            con.Open();
            str = "select * from employee";
            com = new OleDbCommand(str, con);
            oledbda = new OleDbDataAdapter(com);
            ds = new DataSet();
            oledbda.Fill(ds, "employee");
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "employee";
            con.Close();
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection(ConnectionString);
            con.Open();
            str = "select * from employee where empname like '" + textBox1.Text + "%'";
            com = new OleDbCommand(str, con);
            oledbda = new OleDbDataAdapter(com);
            ds = new DataSet();
            oledbda.Fill(ds, "employee");
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "employee";
            con.Close();
        }
    }
}

 
 
Thanks

If it helps you plz mark it as Accepted answer
David Carter
posted  31 posts
since  Jul 22, 2010 
from  Australia

 Re: Use Variable in query
  Posted on: 24 Aug 2010        0  

Thanks for your efforts.
I'm using VB.
Regards,
David
 
Carter's Law of Productivity. "The amount of work a person does is inversely proportional to the number of suggestions they offer." - David J Carter
Satyapriya Nayak
posted  2231 posts
since  Mar 24, 2010 
from 

 Re: Use Variable in query
  Posted on: 25 Aug 2010   Accepted Answer     0  
Hi David,

Here is the vb code below:-

Imports System.Data
Imports System.Data.OleDb
Public Class Form1
    Dim ConnectionString As String = System.Configuration.ConfigurationSettings.AppSettings("dsn")
    Dim con As OleDbConnection
    Dim com As OleDbCommand
    Dim oledbda As OleDbDataAdapter
    Dim ds As DataSet
    Dim str As String
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        bind()
    End Sub
    Sub bind()
        con = New OleDbConnection(ConnectionString)
        con.Open()
        Str = "select * from employee"
        com = New OleDbCommand(Str, con)
        oledbda = New OleDbDataAdapter(com)
        ds = New DataSet()
        oledbda.Fill(ds, "employee")
        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "employee"
        con.Close()
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        con = New OleDbConnection(ConnectionString)
        con.Open()
        str = "select * from employee where empname like '" & TextBox1.Text & "%'"
        com = New OleDbCommand(Str, con)
        oledbda = New OleDbDataAdapter(com)
        ds = New DataSet()
        oledbda.Fill(ds, "employee")
        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "employee"
        con.Close()
    End Sub
End Class



Thanks

If it helps you plz mark it as Accepted answer


David Carter
posted  31 posts
since  Jul 22, 2010 
from  Australia

 Re: Use Variable in query
  Posted on: 25 Aug 2010        0  

Thank you very much.
The line I needed was:
OleDbDataAdapter1.SelectCommand.CommandText =
"Select * From InfringementRecords Where([INFStudent] = '" & SelectName & "')"
I was missing the ' after the = sign and before the )"
The % seemed to be incorrect.
Regards,
David
 
Carter's Law of Productivity. "The amount of work a person does is inversely proportional to the number of suggestions they offer." - David J Carter
       
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 5.2011.3.12
 © 1999 - 2012  Mindcracker LLC. All Rights Reserved