Click here to Skip to main content
15,911,132 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm newer to c#.I'm creating datatable(sql) which has 6 columns.Here is my table definition

spiceID varchar not allow null (primary key)
spiceName text not allow null
mfddate Datetime not allow null
expdate Datetime not allow null
storeNo varchar not allow null
storeamount float not allow null
priceper1kg float not allow null


here I not allow nulls.But If i not insert value to above columns it won't show any error msg & accept what i entre.
plz help me to solve this quickly

Here is my code.I'm creating store keeping data project


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 Ruhunu_spices_PVT_LTD
{
public partial class Product : Form
{
SqlConnection conn1 = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Documents and Settings\\Sachintha\\Desktop\\Ruhunu spices(PVT)LTD\\Ruhunu spices(PVT)LTD\\Rspices.mdf;Integrated Security=True;User Instance=True");
public Product()
{
InitializeComponent();
}

private void Product_Load(object sender, EventArgs e)
{

}

private void button6_Click(object sender, EventArgs e)
{
this.Hide();

login_view f2 = new login_view();
f2.ShowDialog();

}

private void button1_Click(object sender, EventArgs e)
{
SqlCommand comm1 = new SqlCommand("INSERT INTO productT(spiceID,spiceName,MFDdate,EXPdate,storeNo,storeAmount,price1kg)Values(@textBox1,@comboBox1,'" + dateTimePicker1.Value.Date + "','" + dateTimePicker2.Value.Date + "',@textBox2,@textBox3,@textBox4)", conn1);

comm1.Parameters.Add("@textBox1", textBox1.Text);
comm1.Parameters.Add("@comboBox1", comboBox1.Text);
comm1.Parameters.Add("@textBox2", textBox2.Text);
comm1.Parameters.Add("@textBox3", textBox3.Text);
comm1.Parameters.Add("@textBox4", textBox4.Text);




conn1.Open();
comm1.ExecuteNonQuery();
MessageBox.Show("one row inserted");

textBox1.Clear();
textBox2.Clear();
comboBox1.Text = "";
textBox3.Clear();
textBox4.Clear();
dateTimePicker1.Value = DateTime.Today;
dateTimePicker2.Value = DateTime.Today;


conn1.Close();

}

private void button5_Click(object sender, EventArgs e)
{
String commandstring;
commandstring =string.Format( "SELECT * FROM productT WHERE spiceID='{0}'", textBox1.Text.Trim());
SqlDataReader dr1 = null;
SqlCommand com2 = new SqlCommand(commandstring, conn1);
try
{
conn1.Open();
}
catch
{
MessageBox.Show("error in seaching", "message", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
}
dr1 = com2.ExecuteReader();


if (dr1.Read())
{
comboBox1.Text = dr1[1].ToString();
dateTimePicker1.Value = (DateTime)dr1[2];
dateTimePicker2.Value = (DateTime)dr1[3];
textBox2.Text = dr1[4].ToString();
textBox3.Text = dr1[5].ToString();
textBox4.Text = dr1[6].ToString();







}
else
{
MessageBox.Show("Record can not found");
}
dr1.Close();
conn1.Close();

}
}
}
Posted
Updated 2-Sep-13 17:20pm
v2
Comments
ArunRajendra 2-Sep-13 0:02am    
How are you inserting the data?
sachinthasri 4-Sep-13 12:14pm    
through a windows form.This is the cord view of that windows form

I guess its inserting empty string. If you want to insert null you need to check if the controls is empty then explicitly pass DBNull.
 
Share this answer
 
Well, since you didn't provide the code you're using to set this null value and write it to the database...

A null is C# is not the same a database null, or DbNull. Without seeing your code, it's impossible to tell you how to fix it without blindly guessing.
 
Share this answer
 
Comments
sp_suresh 2-Sep-13 0:13am    
Can't get ur question properly ur header and contain text are different
Please tell us clearly and if possible with code
Dave Kreskowiak 2-Sep-13 0:34am    
Be careful who you reply to because you just replied to the wrong person.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900