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:
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)
{
}
}
}