الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

Inserting recoreds into database

بدأه khalil Albaz في 28 نوفمبر 2012 · 5 رد · 618 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

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

{

}

}

}

}

#2

When does the error happen? In which event the exception is fired?

3 −1

أدعو الله أن يجعل أعمالنا خالصة لوجهه الكريم --- وأن ينفعنا بما علمنا

Dr Sayed Tohamy

Programming Instructor

مدرب برمجة

---------------

EmaSoft2000

-------------------

#3
ema2000_t كتب:

When does the error happen? In which event the exception is fired?

there is no error no exceptons

all what happens is just it overwrite the old data with the new inserted one

some kind of replacement

thx anyway

#4

according to your code, every click on the add button will do two actions

first is inserting a record into the database

second calling the method loaddata this method selects all records from the database and add them into the combobox

but it does not clear the old items in the combpbox so you may need to add this line to the top of this method

comboBox1.Items.Clear();

in addition, the code in the comboBox1_SelectedIndexChanged has no significance, it does no thing

I think you want another code like button1.PerformClick(); to display the data immediately into the datagridview

I hope this is helpfull

thnks

تم تعديل هذه المشاركة بواسطة ema2000_t في 4 ديسمبر 2012 في 04:01

3 −1

أدعو الله أن يجعل أعمالنا خالصة لوجهه الكريم --- وأن ينفعنا بما علمنا

Dr Sayed Tohamy

Programming Instructor

مدرب برمجة

---------------

EmaSoft2000

-------------------

#5
ema2000_t كتب:

according to your code, every click on the add button will do two actions

first is inserting a record into the database

second calling the method loaddata this method selects all records from the database and add them into the combobox

but it does not clear the old items in the combpbox so you may need to add this line to the top of this method

comboBox1.Items.Clear();

in addition, the code in the comboBox1_SelectedIndexChanged has no significance, it does no thing

I think you want another code like button1.PerformClick(); to display the data immediately into the datagridview

I hope this is helpfull

thnks

#6

First of all thx for reading the entire code

as you said once the user click the add button

it will insert a new recored to the database then it will call the loadData to fill the comboBox

and display it.

what I want is to display the old data and also the new inserted one

just to make sure that I have inserted the data correctly to the database

there is no need to clear the comboBox

the problem is that the old data in the database is overwriten by the new inserted one

it does not make any sense

the code is working well

the problem could might be with the database itself

have you ever seen like this

thx any way

it was nice from you to read my code

cause for a second I thought that the member really do not read the code

thx again

best wishes

مواضيع مشابهة