saeed razaee
چهارشنبه 03 مهر 1387, 09:47 صبح
با سلام
دوستان عزيز من در يك فرم از دو باكس براي ذخيره اطلاعات در بانك sql استفاده مي كنم و وقتي روي ذخيره كليك مي كنم در DataGridView اطلاعات ثبت شده دوبار نمايش داده مي شود در صورتي كه در بانك عملاً يك بار ذخيره شده است و كد واحد كليد اصلي مي باشد.در شكل پيوست واضح تر مي باشد. كدهاي استفاده شده در برنامه در زير آمده است.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace peshnahad
{
public partial class Form1 : Form
{
SqlConnection objConnection = new SqlConnection("Server =ROSTAMPOUR;database=peshnahad;Integrated Security=True");
SqlDataAdapter objDataAdapter = new SqlDataAdapter();
DataSet obgDataset = new DataSet();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
objDataAdapter.SelectCommand = new SqlCommand();
objDataAdapter.SelectCommand.Connection = objConnection;
objDataAdapter.SelectCommand.CommandText =
"SELECT cod_vahed,nam_vahed" + " FROM vahedsazman";
objDataAdapter.SelectCommand.CommandType = CommandType.Text;
objConnection.Open();
objDataAdapter.Fill(obgDataset, "vahedsazman");
objConnection.Close();
grdAuthorTitle.AutoGenerateColumns = true;
grdAuthorTitle.DataSource = obgDataset;
grdAuthorTitle.DataMember = "vahedsazman";
setGridDataSource();
DataGridViewCellStyle objAlignRightCellStyle = new DataGridViewCellStyle();
objAlignRightCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
grdAuthorTitle.Columns["nam_vahed"].HeaderCell.Style = objAlignRightCellStyle;
grdAuthorTitle.Columns["cod_vahed"].HeaderCell.Style = objAlignRightCellStyle;
grdAuthorTitle.Columns[0].Width = 80;
grdAuthorTitle.Columns[1].Width = 255;
DataGridViewCellStyle objAlternatingCellStyle = new DataGridViewCellStyle();
objAlternatingCellStyle.BackColor = Color.LightBlue;
grdAuthorTitle.AlternatingRowsDefaultCellStyle = objAlternatingCellStyle;
grdAuthorTitle.Columns[0].HeaderText = "كد واحد ";
grdAuthorTitle.Columns[1].HeaderText = "نام واحد سازماني ";
}
private void btnsave_Click(object sender, EventArgs e)
{
SqlCommand objCommand = new SqlCommand();
objCommand.Connection = objConnection;
try
{
if (objConnection.State == ConnectionState.Closed)
objConnection.Open();
objCommand.CommandText = "INSERT INTO vahedsazman"
+ "(cod_vahed,nam_vahed) " + "VALUES (@cod_vahed,@nam_vahed)";
objCommand.Parameters.AddWithValue("@cod_vahed", textBox1.Text);
objCommand.Parameters.AddWithValue("@nam_vahed", textBox2.Text);
objCommand.ExecuteNonQuery();
textBox1.Clear();
textBox2.Clear();
textBox1.Focus();
objConnection.Close();
setGridDataSource();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void setGridDataSource()
{
objDataAdapter.SelectCommand = new SqlCommand();
objDataAdapter.SelectCommand.Connection = objConnection;
objDataAdapter.SelectCommand.CommandText = "SELECT cod_vahed,nam_vahed" + " FROM vahedsazman";
objDataAdapter.SelectCommand.CommandType = CommandType.Text;
objConnection.Open();
objDataAdapter.Fill(obgDataset, "vahedsazman");
objConnection.Close();
grdAuthorTitle.AutoGenerateColumns = true;
grdAuthorTitle.DataSource = obgDataset;
grdAuthorTitle.DataMember = "vahedsazman";
}
}
}
دوستان عزيز من در يك فرم از دو باكس براي ذخيره اطلاعات در بانك sql استفاده مي كنم و وقتي روي ذخيره كليك مي كنم در DataGridView اطلاعات ثبت شده دوبار نمايش داده مي شود در صورتي كه در بانك عملاً يك بار ذخيره شده است و كد واحد كليد اصلي مي باشد.در شكل پيوست واضح تر مي باشد. كدهاي استفاده شده در برنامه در زير آمده است.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace peshnahad
{
public partial class Form1 : Form
{
SqlConnection objConnection = new SqlConnection("Server =ROSTAMPOUR;database=peshnahad;Integrated Security=True");
SqlDataAdapter objDataAdapter = new SqlDataAdapter();
DataSet obgDataset = new DataSet();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
objDataAdapter.SelectCommand = new SqlCommand();
objDataAdapter.SelectCommand.Connection = objConnection;
objDataAdapter.SelectCommand.CommandText =
"SELECT cod_vahed,nam_vahed" + " FROM vahedsazman";
objDataAdapter.SelectCommand.CommandType = CommandType.Text;
objConnection.Open();
objDataAdapter.Fill(obgDataset, "vahedsazman");
objConnection.Close();
grdAuthorTitle.AutoGenerateColumns = true;
grdAuthorTitle.DataSource = obgDataset;
grdAuthorTitle.DataMember = "vahedsazman";
setGridDataSource();
DataGridViewCellStyle objAlignRightCellStyle = new DataGridViewCellStyle();
objAlignRightCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
grdAuthorTitle.Columns["nam_vahed"].HeaderCell.Style = objAlignRightCellStyle;
grdAuthorTitle.Columns["cod_vahed"].HeaderCell.Style = objAlignRightCellStyle;
grdAuthorTitle.Columns[0].Width = 80;
grdAuthorTitle.Columns[1].Width = 255;
DataGridViewCellStyle objAlternatingCellStyle = new DataGridViewCellStyle();
objAlternatingCellStyle.BackColor = Color.LightBlue;
grdAuthorTitle.AlternatingRowsDefaultCellStyle = objAlternatingCellStyle;
grdAuthorTitle.Columns[0].HeaderText = "كد واحد ";
grdAuthorTitle.Columns[1].HeaderText = "نام واحد سازماني ";
}
private void btnsave_Click(object sender, EventArgs e)
{
SqlCommand objCommand = new SqlCommand();
objCommand.Connection = objConnection;
try
{
if (objConnection.State == ConnectionState.Closed)
objConnection.Open();
objCommand.CommandText = "INSERT INTO vahedsazman"
+ "(cod_vahed,nam_vahed) " + "VALUES (@cod_vahed,@nam_vahed)";
objCommand.Parameters.AddWithValue("@cod_vahed", textBox1.Text);
objCommand.Parameters.AddWithValue("@nam_vahed", textBox2.Text);
objCommand.ExecuteNonQuery();
textBox1.Clear();
textBox2.Clear();
textBox1.Focus();
objConnection.Close();
setGridDataSource();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void setGridDataSource()
{
objDataAdapter.SelectCommand = new SqlCommand();
objDataAdapter.SelectCommand.Connection = objConnection;
objDataAdapter.SelectCommand.CommandText = "SELECT cod_vahed,nam_vahed" + " FROM vahedsazman";
objDataAdapter.SelectCommand.CommandType = CommandType.Text;
objConnection.Open();
objDataAdapter.Fill(obgDataset, "vahedsazman");
objConnection.Close();
grdAuthorTitle.AutoGenerateColumns = true;
grdAuthorTitle.DataSource = obgDataset;
grdAuthorTitle.DataMember = "vahedsazman";
}
}
}