PDA

View Full Version : سوال: مشکل درج رکورد خالی در جدول



ahmad1010
یک شنبه 01 تیر 1393, 21:39 عصر
لطفا راهنمایی نمایید چکار کنم که اگه تکست باکس1 خالی باشه و اینتر زده بشه مقدار خالی در جدول ثبت نشه؟ ممنون

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{



if (e.KeyCode == Keys.Enter)
{
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Documents and Settings\\Administrator\\My Documents\\Visual Studio 2008\\Projects\\WindowsFormsApplication3\\WindowsF ormsApplication3\\bin\\Debug\\data.mdb";

OleDbConnection myConnection = new OleDbConnection(connectionString);
myConnection.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = myConnection;




OleDbDataAdapter da = new OleDbDataAdapter("select * from t2 where sabt like '%" + textBox1.Text + "%'", myConnection);


DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
MessageBox.Show("شماره ثبت تکراری است", "توجه", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox1.Focus();
}
else
{


OleDbCommand cmd1 = new OleDbCommand();
cmd1.Connection = myConnection;
cmd1.CommandText = "insert into t2 ([sabt])values(@sabt)";
cmd1.Parameters.AddWithValue("@sabt", textBox1.Text);
cmd1.ExecuteNonQuery();


string s = string.Format("*93{0}*", textBox1.Text);
cmd.CommandText = "insert into t1 ([sabt])values(@sabt)";
cmd.Parameters.AddWithValue("@sabt", s);

cmd.ExecuteNonQuery();

textBox1.Text = "";

}
}
}

parvizwpf
دوشنبه 02 تیر 1393, 01:22 صبح
اول بخش else میتونید یک شرط بذارید و خالی بودن تکست رو بررسی کنید:

else
{
if(string.IsNullOrEmpty(textBox1.Text){

.
.
.
.
}