Hi every body
I need your help in windows application C#
when I add data to the database using insert statment the recored is inserted but when add a new recored (row) there is no previous data
what is the prpblem here is my 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.SqlClient;
using System.Configuration;
namespace SearchComboBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int n = 0;
private void Form1_Load(object sender, EventArgs e)
{
label2.Hide();
loadData();
comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
}
private void button1_Click(object sender, EventArgs e)
{
label2.Text = comboBox1.SelectedItem.ToString();
label2.Show();
string conStr = ConfigurationManager.ConnectionStrings["SearchComboBox.Properties.Settings.SearchComboBoxConnectionString"].ToString();
SqlConnection con = new SqlConnection(conStr);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("select * from Search where name='" + label2.Text + "'", con);
da.Fill(dt);
con.Close();
dataGridView1.DataSource = dt;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.SelectedItem = comboBox1.SelectedItem.ToString();
}
private void Add_Click(object sender, EventArgs e)
{
string conStr = ConfigurationManager.ConnectionStrings["SearchComboBox.Properties.Settings.SearchComboBoxConnectionString"].ToString();
SqlConnection con = new SqlConnection(conStr);
if (!String.IsNullOrEmpty(txtName.Text) && !String.IsNullOrWhiteSpace(txtName.Text))
{
con.Open();
SqlCommand cmd = new SqlCommand("insert into Search(name)values('" + txtName.Text + "')", con);
int n = (int)cmd.ExecuteNonQuery();
con.Close();
if (n > 0)
{
MessageBox.Show("name is added");
}
else
{
MessageBox.Show("the name can't be added");
}
}
else
{
MessageBox.Show("please enter name to be added");
}
loadData();
}
public void loadData()
{
string conStr = ConfigurationManager.ConnectionStrings["SearchComboBox.Properties.Settings.SearchComboBoxConnectionString"].ToString();
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand("select * from Search", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
try
{
while (dr.Read())
{
comboBox1.Items.Add(dr["name"].ToString());
}
dr.Dispose();
dr.Close();
con.Close();
}
catch
{
}
}
}
}