添加一些代码以在gridview中显示数据 [英] add some code to display data in gridview
本文介绍了添加一些代码以在gridview中显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想修改我的代码以在数据gridview中显示插入的数据...
我的代码是
i want to modify my code to display inserted data in data gridview...
My code is
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;
namespace employee
{
public partial class details : Form
{
public details()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string empid = textBox1.Text;
string name = textBox2.Text;
string designation = textBox3.Text;
string salary = textBox6.Text;
string value = "";
bool isChecked = radioButton1.Checked;
if (isChecked)
value = radioButton1.Text;
else
value = radioButton2.Text;
string address = textBox4.Text;
string pincode = textBox5.Text;
string prifix = textBox8.Text;
string phone = textBox7.Text;
string email = textBox9.Text;
if (empid == "" || name == "" || designation == "" || salary == "" || address == "" || pincode == "" || prifix == "" || phone == "" || email == "")
{
MessageBox.Show("Please ensure all fields are entered", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
System.Data.SqlClient.SqlConnection sqlcommand1 = new System.Data.SqlClient.SqlConnection("Persist Security Info=True;User ID=*******;Password=******;Initial Catalog=ubsemp;Server=****");
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "IF NOT EXISTS(SELECT * FROM UEMPLOYEE WHERE EMPLOYEE_ID=@EMPLOYEE_ID) INSERT INTO UEMPLOYEE(EMPLOYEE_ID,NAME,DESIGNATTION,GENDER,ADDRESS,PINCODE,SALARY,PHPREFIX,PHONE,EMAIL) VALUES(@EMPLOYEE_ID,@NAME,@DESIGNATTION,@GENDER,@ADDRESS,@PINCODE,@SALARY,@PHPREFIX,@PHONE,@EMAIL)";
cmd.Parameters.AddWithValue("@EMPLOYEE_ID", empid);
cmd.Parameters.AddWithValue("@NAME", name);
cmd.Parameters.AddWithValue("@DESIGNATTION", designation);
cmd.Parameters.AddWithValue("@SALARY", salary);
cmd.Parameters.AddWithValue("GENDER", value);
cmd.Parameters.AddWithValue("@ADDRESS", address);
cmd.Parameters.AddWithValue("PINCODE", pincode);
cmd.Parameters.AddWithValue("PHPREFIX", prifix);
cmd.Parameters.AddWithValue("PHONE", phone);
cmd.Parameters.AddWithValue("@EMAIL", email);
cmd.Connection = sqlcommand1;
sqlcommand1.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("successfully inserted");
}
}
}
}
推荐答案
创建如下方法并调用它插入完成后
create method like below and call it after you insert complete
private void Loademployees()
{
try
{
using(SqlConnection connection = new SqlConnection(connetionString))
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter("select * from UEMPLOYEE", connection);
adapter.Fill(ds);
connection.Close();
dataGridView1.DataSource = ds.Tables[0];
}
}
catch (Exception ex)
{
MessageBox.Show (ex.ToString());
}
}
您好,
成功插入后,您需要提取数据库中的数据。然后把它展示给girdview。
试试这种方式。
Hello,
After successful insertion , you have to fetch data from database . and then show it to girdview .
Try this way.
SqlConnection con=new SqlConnection (connectionstring);
con.Open();
SqlDataAdapter da=new SqlDataAdapter("select * from UEMPLOYEE",con);
DataSet ds=new DataSet();
da.Fill(ds);
con.Close();
datagridview.DataSource=ds.Table[0]; //now set gridview DataSource
谢谢
thanks
这篇关于添加一些代码以在gridview中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文