Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I would like some help with the steps to follow and coding if possible. My project is to create a small dictionary software (standalone - maybe 1 form or two max) using a database I have set up in SQL server (Two fields, name and definition) with approximately 150 words. My form application in Visual Studio will have a textbox1 (Where the user writes a word) a search button and DataGridview (where the definition to that word will show up).

I need please help rectifying my coding, I have used many approaches on SQL connection string using c# , i am facing errors in the syntax. Most of the approaches tell you how to manage complex data as Adding, editing, ect.. My project purpose is very simple, My data is stored on SQL and all I want is to display the result in a simple way that will save me time performing my work.

Info:
Database/ initial catalogue ; Raffa
Server: Localhost or Raffa\Rk
Table name : Dictionnary

Erros; Sqlcmd and textbox1 does not exist in the current context

I used different codes fors establishing a connection and bug free but the query when I run the app did not show any result in the textbox2/datagridview. I had also an error ''connection to Sql database"


My question is as follow:

- Isn't there any simple code to perform these tasks?

1-Search text input in textbox1 in the table
2-Fecth data in the definition field
3-Display it in the Textbox2? ( Should I use Textbox2 or Datagridview? )

Should the connection string establish under the ''buttonclick'' or events..?

I don't want to add or edit or cuztomize anything, As I will have all my data updated from SQL before building the application.

Thanks alot,


Current Code:


C#
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.Sql;
using System.Data.SqlClient;



namespace strongdic
{

    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }


        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connectionString = "Initial Catalog=main;Data Source=(local);Integrated Security=SSPI;";
            SqlConnection cn = new SqlConnection(connectionString);
            string sCommand = "SELECT definition FROM Dictionnary WHERE word ='"+ TextBox1.Text.Trim()+"'",Sqlcon;
            dataGridView1.Text = Sqlcmd.ExecuteScalar().ToString();

        }


        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
Posted
Updated 20-Jun-12 0:22am
v3
Comments
J.Karthick 19-Jun-12 11:02am    
Nice Approach buddy !!

Will explain my level best.. :)
rajakhoury 20-Jun-12 6:29am    
Hello jKarthick, I have tried your approach but does it work, or maybe I did not execute it well.. please have a look. thanks
J.Karthick 22-Jun-12 6:03am    
You using datagridview ?? !
I thought you using textbox. As a beginner, better you use textbox first to learn.

Problem in
"SqlConnection cn = new SqlConnection(connectionString);

string sCommand = "SELECT definition FROM Dictionnary WHERE word ='"+ TextBox1.Text.Trim()+"'",Sqlcon;

dataGridView1.Text = Sqlcmd.ExecuteScalar().ToString();"

you need to replace it with,

SqlConnection Sqlcon = new SqlConnection(connectionString);
Sqlcon.Open();
SqlCommand sqlcmd = new SqlCommand("SELECT definition FROM Dictionnary WHERE word ='"+ TextBox1.Text.Trim()+"'",Sqlcon);

Textbox2.Text = sqlcmd.ExecuteScalar().ToString();

Sqlcon.Close();

(If it shows error in "Sqlcon.Open()" then Check your connectionstring)
It should be like
"Data Source=Raffa\Rk;Initial Catalog=Raffa;Integrated Security=True"

Hope it helps you !! :-)
Get back to me if you face any trouble...
Sandeep Mewara 19-Jun-12 11:32am    
this forum is for more advanced users, but it's never late to learn.
Incorrect. This forum is for everyone who tries, makes effort and really get stuck somewhere.

Just posting a homework question or asking for code is not encouraged.

As for you, if you share what you have tried and share the errors, it might be more useful.
rajakhoury 20-Jun-12 6:24am    
hello Sandeep, Would you please have a look at my progress. Thanks

1 solution

You don't need to link the textboxes with sql.

In design, While you double clicking the search button, it will send you to an block named CLICK EVENT.

Inside that you need to get your textbox1 value and pass that value to sql through SQL QUERY.


To establish Connection with SQL follow the steps
Hope you added
using system.Data.Sql;
using system.Data.SqlClient;
At the beginning of the (.cs) file,(Where the EVENTS found)

Inside Class,
Sqlconnection Sqlcon = new Sqlconnection("Server=XXXXX;Database=YYYY;uid=sa;password=ZZZZ");

Here X, Y, Z are the respective SQL config.
for checking whether the connection is working or not. Just double click on design form which you have textboxes, it will move you to an PAGE_LOAD event.
Inside that just type,

Sqlcon.open();
Sqlcon.close();


if the connection is proper, then it will not throw any errors while you check run the solutions.


To search an Word from SQL

* Inside BUTTON CLICK EVENT of SEARCH

type the following

Sqlcommand Sqlcmd = new Sqlcommand("select FIELDNAME from TABLE_NAME where SEARCHFIELD = '"+ textbox1.Text.Trim() +"'",Sqlcon);

textbox2.Text = Sqlcmd.ExecuteScalar().ToString();



Thats all,
Just run the code and tell me if you found any errors :-)
HAPPY CODING :-)
Apology me if you found struggle in reading my LANGUAGE ;-)
 
Share this answer
 
Comments
rajakhoury 19-Jun-12 13:51pm    
@jkarthick1989 : thank you alot for the help. I will try to 'read' your language and I hope it works out well. :)

I will will let you know about my progress. thanks again!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900