Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Web;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Configuration;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        OleDbConnection con;
        OleDbCommand cmd;
        OleDbDataAdapter da;
        DataSet ds;

        public Form1()
        {
            InitializeComponent();
          
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database11.accdb");
            cmd = new OleDbCommand("select * from Table1", con);
            da = new OleDbDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "Table1");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str = "";
            if (checkBox1.Checked == true)
            {
                str = str + "," + checkBox1.Text;
            }
            if (checkBox2.Checked == true)
            {
                str = str + "," + checkBox2.Text;
            }
            if (checkBox3.Checked == true)
            {
                str = str + "," + checkBox3.Text;
            }
            if (checkBox4.Checked == true)
            {
                str = str + "," + checkBox4.Text;
            }
            if (checkBox5.Checked == true)
            {
                str = str + "," + checkBox5.Text;
            }
            if (checkBox6.Checked == true)
            {
                str = str + "," + checkBox6.Text;
            }
            if (checkBox7.Checked == true)
            {
                str = str + "," + checkBox7.Text;
            }
            if (checkBox8.Checked == true)
            {
                str = str + "," + checkBox8.Text;
            }
            label1.Text = str;
         
          
                con.Open();
                cmd.CommandText = "insert into Table1(ID,medNo) values("+int.Parse(textBox1.Text)+",'"+str+"')";
                cmd.ExecuteNonQuery();
                da.Update(ds, "Table1");
                MessageBox.Show("data inserted");
                con.Close();
           
           
        }

      
       
    }
}
Posted
Updated 13-Mar-15 0:15am
v3
Comments
Leo Chapiro 13-Mar-15 5:29am    
Try to debug you source code dude, I've heard it helps sometimes ;)
MayankSemwal 13-Mar-15 5:33am    
i did that friend,its always get stuck to same error..
Thanks7872 13-Mar-15 5:49am    
And why on earth do you think that we can read your mind? Who knows where the error is? Put breakpoint on the line you got the error at. Look for the object which is null. Try to make your code such that it should not be null at that point.

cmd.CommandText.......

What is cmd here?
MayankSemwal 13-Mar-15 5:55am    
OleDbCommand cmd;
Thanks7872 13-Mar-15 5:57am    
Where is it declared? Did you try steps i suggested you in previous comment? If yes, what did you observe?

Did you mean to write this?

C#
if (!String.IsNullOrWhiteSpace(str))


To check, if the string is not a null or white space and then execute the command.

Also you should always create parameterized commands, this code is viable and exposed to SQL Injection commands and can cause you data loss. You can read on how to pass parameters, instead of string concatenation on MSDN[^].

Edit

I am sorry for getting too late for getting back to you, because I have to face electric supply failure too much here in my country.

Now, since you've provided the code that you're using. You can see there are 2 possible places where this error might generate, the textBox1.Text, and the str. Since that check I provided didn't work, you need to check for the value of textBox1 if that is also not null.

Again, as many have also said... You should try out debugger to see which of these variables is null at the run-time. You can create breakpoints on the lines (I would add that on the function call to see the function executing in front of my sight) and then press F11 to continue executing statements. This way, at every stage, you will be able to see which of the objects are null, and which objects have a value and thus you can fix it.
 
Share this answer
 
v3
Comments
Santosh K. Tripathi 13-Mar-15 5:34am    
+5 from me.

but i'll say, give some time to Mayank to learn by debugging.
MayankSemwal 13-Mar-15 6:31am    
help me out because been trying from 55 minutes.
Afzaal Ahmad Zeeshan 13-Mar-15 6:42am    
Please see the update to my answer.
Afzaal Ahmad Zeeshan 13-Mar-15 6:42am    
Thanks for your vote Santosh.

I have also added that to my answer, to let OP make his a decision to test the application in running mode using debugger.
MayankSemwal 13-Mar-15 6:49am    
Afzaal,i am doing it even at debugging mode its again get stuck at con.open().
namespace windowfromapplication1
{
public partial class registration : Form
{
C#
private void button3_Click(object sender, EventArgs e)
{
    string str = "";
    if (checkBox1.Checked)
    {
        str += checkBox1.Text+",";
    }
    if (checkBox2.Checked)
    {
        str += checkBox2.Text + ",";
    }
    if (checkBox3.Checked)
    {
        str += checkBox3.Text + ",";
    }
    if (checkBox4.Checked)
    {
        str += checkBox4.Text + ",";
    }
    if (checkBox5.Checked)
    {
        str += checkBox5.Text + ",";
    }
    if (checkBox6.Checked)
    {
        str += checkBox6.Text + ",";
    }
    if (checkBox7.Checked)
    {
        str += checkBox7.Text + ",";
    }
    if (checkBox8.Checked)
    {
        str += checkBox8.Text + ",";
    }
    else
    {
        MessageBox.Show("select medicine");
    }

    DataRow drNewRow = dtMedicineParticular.NewRow();

    drNewRow["medName"] = str;
    drNewRow["medQty"] = txtPackets.Text;

    dtMedicineParticular.Rows.Add(drNewRow);

    gvMedicineParticulars.DataSource = dtMedicineParticular;

    //ddlMedName.Text = "";
    txtPackets.Text = "";
 foreach (DataGridViewRow drvMedicine in gvMedicineParticulars.Rows)
                {
                    MedNo = drvMedicine.Cells[0].Value.ToString();
                    MedPacket = drvMedicine.Cells[1].Value.ToString();

                    string insertQriTreat = "Insert into Treatment_Details(Treat_Med_No,Treat_Issue_Date,Treat_Qty,Treat_Due_Date,Treat_P_ID,Treat_P_Pkg) values ('" + MedNo + "',#" + IssueDateMed + "#," + MedQty + ",#" + dueDate + "#," + PatientID + "," + MedPacket + ")";

                    DBT.exeQuery(insertQriTreat);
                }
}
 
Share this answer
 

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