اين يك برنامه Messaging هست كه زمان دانشگاه نوشتم.
خيلي sentax پيچيده اي نداره و جاي پيشرفت داره.
فقط براي اجرا DB كه در فولدر DataBase هست را در SQL ايمپورت كنيد.
انشاا... بقيه برنامه ها را هم ميزارم.
موفق باشيد.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Messaging
{
public partial class Messages : Form
{
public static bool user;
public Messages()
{
InitializeComponent();
}
SqlCommand cmd;
SqlDataAdapter da;
DataSet ds;
DateTime time = DateTime.Now;
/////////////////Send
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == string.Empty)
{
MessageBox.Show("Please write the receiver", "warning");
}
else
{
cmd = new SqlCommand("select UserName from login where UserName='" + textBox1.Text + "'", new MyConnection().cnn);
SqlDataReader dr;
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
cmd = new SqlCommand("insert into Inbox values('" + Login.user + "','" + textBox1.Text + "','" + textBox2.Text + "','" + textBox4.Text + "','" + time + "')", new MyConnection().cnn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
cmd = new SqlCommand("insert into Outbox values('" + textBox1.Text + "','" + Login.user + "','" + textBox2.Text + "','" + textBox4.Text + "','" + time + "')", new MyConnection().cnn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
this.BindGrid1();
this.BindGrid2();
MessageBox.Show("Your Massage has send successfully", "Info");
textBox1.Text = "";
textBox2.Text = "";
textBox4.Text = "";
dataGridView2.Refresh();
button3.Enabled = true;
}
}
else
{
MessageBox.Show("There isn't this receiver", "warning");
textBox1.Text = "";
textBox2.Text = "";
textBox4.Text = "";
}
}
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox4.Text = "";
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Dispose();
Login log = new Login();
log.ShowDialog();
}
private void Messages_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void Messages_Load(object sender, EventArgs e)
{
lblName.Text = "Welcome : " + Login.user;
dataGridView1.Visible = false;
button5.Enabled = false;
dataGridView2.Visible = false;
button3.Enabled = false;
this.BindGrid1();
this.BindGrid2();
}
public void BindGrid1()
{
cmd = new SqlCommand("select * from Outbox where Sender='" + Login.user + "'", new MyConnection().cnn);
SqlDataReader dr;
dr = cmd.ExecuteReader();
if (!dr.HasRows)
{
dataGridView1.Visible = false;
button5.Enabled = false;
}
else
{
da = new SqlDataAdapter("select ID as 'ID' ,Sender,Subject,Messages,date from Inbox where Receiver='" + Login.user + "'", new MyConnection().cnn);
ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.Visible = true;
button5.Enabled = true;
}
}
public void BindGrid2()
{
cmd = new SqlCommand("select * from Outbox where Sender='" + Login.user + "'", new MyConnection().cnn);
SqlDataReader dr;
dr = cmd.ExecuteReader();
dr.Read();
if (!dr.HasRows)
{
dataGridView2.Visible = false;
button3.Enabled = false;
}
else
{
da = new SqlDataAdapter("select ID as 'ID' ,Receiver as 'To' ,Subject,Messages,date from Outbox where Sender='" + Login.user + "'", new MyConnection().cnn);
ds = new DataSet();
da.Fill(ds);
dataGridView2.DataSource = ds.Tables[0];
dataGridView2.Visible = true;
button3.Enabled = true;
}
}
private void dataGridView2_CurrentCellChanged(object sender, EventArgs e)
{
try
{
textBox5.Text = dataGridView2.CurrentRow.Cells["messages"].Value.ToString();
}
catch (Exception ex)
{ }
}
private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
{
try
{
textBox3.Text = dataGridView1.CurrentRow.Cells["messages"].Value.ToString();
}
catch (Exception ex)
{ }
}
private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button5_Click(object sender, EventArgs e)
{
try
{
label3.Text = dataGridView1.CurrentRow.Cells["ID"].Value.ToString();
cmd = new SqlCommand("delete from inbox where id=" + Convert.ToInt16(label3.Text) + "", new MyConnection().cnn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
textBox3.Text = "";
}
catch (Exception ex1)
{ }
this.BindGrid1();
}
private void button3_Click(object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show("Are you sure to Delet this Message?", "Warning",
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning);
if (dr == DialogResult.Yes)
{
try
{
label4.Text = dataGridView2.CurrentRow.Cells["ID"].Value.ToString();
cmd = new SqlCommand("delete from Outbox where id=" + Convert.ToInt16(label4.Text) + "", new MyConnection().cnn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
textBox5.Text = "";
}
catch (Exception ex1)
{ }
this.BindGrid2();
}
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
About a = new About();
a.ShowDialog();
}
}
}