3 Replies - 958 Views - Last Post: 08 November 2012 - 08:18 AM Rate Topic: -----

#1 Dak Group  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-November 12

the code is suppose to insert data in sql database

Posted 08 November 2012 - 12:29 AM

 private void button2_Click(object sender, EventArgs e)
        {
             string DonorType = "";
             string DonatedBlood = "";
             string VoluntaryDonor = "";
             string DonationPeriod = "";
         if (txtrid.Text == "" || txtrid.Text == null)
                MessageBox.Show("Please enter Registration Id");
         else
         {
             if (txtrid.Text != "" && txtfname.Text != "" && txtlname.Text != "" && txtage.Text != "" && txtadd.Text != "" && txttno.Text != "")
             {
             if (rdBtnVoluntary.Checked)
                 DonorType = "Voluntary";
             else if (rdBtnReplacement.Checked)
                 DonorType = "Replacement";
             else if (rdBtnAuto.Checked)
                 DonorType = "AutoLogus";
             else if (rdBtnDirect.Checked)
                 DonorType = "Direct";
             if (radBtnDonatedYes.Checked)
                 DonatedBlood = "Yes";
             else if (radBtnDonatedNo.Checked)
                 DonorType = "No";
             if (rdBtnVoluntaryYes.Checked)
                 VoluntaryDonor = "Yes";
             else if (rdBtnVoluntaryNo.Checked)
                 VoluntaryDonor = "No";
             if (rdBtn3M.Checked)
                 DonationPeriod = "3M";
             else if (rdBtn6M.Checked)
                 DonationPeriod = "6M";
             else if (rdBtn12M.Checked)
                 DonationPeriod = "12M";
             }

         }
SqlConnection cn = new SqlConnection(@"Data Source=DARSHAN-PC\SQLEXPRESS;Initial Catalog=BBMS;Integrated Security=True");
           
         SqlDataReader dr;

            cn.Open();
           
          
            string sQuery = "insert into reg_details(registration_id,first_name,middle_name,last_name,age,donor_address,telephone_number,mobile_number,date,occupation,blood_group,patient_name,ward,gender,last_date_of_donation) values('" + txtrid.Text + "','" + txtfname.Text + "','" + txtmname.Text + "','" + txtlname.Text + "','" + txtage.Text + "','" + txtadd.Text + "','" + txttno.Text + "','" + txtmno.Text + "','" + txtdate.Text + "','" + txtocc.Text + "','" + comboBox1.Text + "','" + txtpname.Text + "','" + txtward.Text + "','" + comboBox2.Text + "','" + txtldd.Text + "')";
            SqlCommand cmd = new SqlCommand(sQuery);
            cmd.Connection = cn;
            try {
                cmd.ExecuteNonQuery();
             }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                 throw ex;
                }
            finally 
            {
                cn.Close();
            }
            MessageBox.Show("inserted");
}





is the code for radio button correct?

the error is: cannot put value null to column donor type,table 'BBMS.dbo/reg_details';columns does not allow nulls.INSERT fails.

Is This A Good Question/Topic? 0
  • +

Replies To: the code is suppose to insert data in sql database

#2 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: the code is suppose to insert data in sql database

Posted 08 November 2012 - 01:38 AM

Firstly, take a look at this, which explains how to correctly parameterize your SQL queries.

Secondly, you're attempting an Insert statement without inserting a value into the Donor type field (just like the error says). That means you're attempting to insert NULL into a non-nullable column.
Was This Post Helpful? 2
  • +
  • -

#3 coder3788  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 62
  • Joined: 06-November 12

Re: the code is suppose to insert data in sql database

Posted 08 November 2012 - 02:04 AM

Imagine you have a table holding the top 100 ATP Tennis Players. In this table you have a column named Coach,which holds the current coach of the player. Some players maybe just sacked their coach, have none and are looking to hire someone.
What value would you hold in the Coach column for them? If you put an empty string, it doesn't mean that they have no coach, it means they are coached by a guy with no name or named ''.
To solve that, columns can hold null values. Null means empty, nothing, no element at all.
Columns can be nullable or not. In SQL Management Studio (if you're using one) there is a checkbox for each column in Table Design allowing you to set it.

In conclusion, if you feel that your problem column holds info that the entity the table is modeling can lack in some situations , set it to nullable. If not, insert meaningful data in it so you won't get an exception.

Notice that if you insert empty strings in the Coach column and you make a report grouping the players by coach, each coach will have one player ,except for the no-name empty string coach, which coaches all players that don't have one. This fails to model reality, which is why databases are used in the first place.
Was This Post Helpful? 2
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5512
  • View blog
  • Posts: 11,817
  • Joined: 02-June 10

Re: the code is suppose to insert data in sql database

Posted 08 November 2012 - 08:18 AM

Please tell us this is a homework project and that you aren't trying to build something for actual deployment in a working medical office. This is the kind of thing we expect from first semester students. Which if you are a student doing homework means you are right on track. If you think this is going to fulfil a paying contract or that you might use this to audition your way in the door for a medical business, is a scary thing.


It is littered with lots of direct use of textbox values.

string sQuery = "insert into reg_details(registration_id,first_name,middle_name,last_name,age,donor_address,telephone_number,mobile_number,date,occupation,blood_group,patient_name,ward,gender,last_date_of_donation) values('" + txtrid.Text + "','" + txtfname.Text + "','" + txtmname.Text + "','" + txtlname.Text + "','" + txtage.Text + "','" + txtadd.Text + "','" + txttno.Text + "','" + txtmno.Text + "','" + txtdate.Text + "','" + txtocc.Text + "','" + comboBox1.Text + "','" + txtpname.Text + "','" + txtward.Text + "','" + comboBox2.Text + "','" + txtldd.Text + "')";


You want to be using properties for these things, that change when the textbox value change. The goal is to separate the logic from the GUI as much as possible. Read this:
Separating data from GUI - PLUS - serializing the data to XML

When you see a godawful mess of if...else...if like this
if (rdBtnVoluntary.Checked)
14                 DonorType = "Voluntary";
15             else if (rdBtnReplacement.Checked)
16                 DonorType = "Replacement";
17             else if (rdBtnAuto.Checked)
18                 DonorType = "AutoLogus";
19             else if (rdBtnDirect.Checked)
20                 DonorType = "Direct";
21             if (radBtnDonatedYes.Checked)
22                 DonatedBlood = "Yes";
23             else if (radBtnDonatedNo.Checked)
24                 DonorType = "No";
25             if (rdBtnVoluntaryYes.Checked)
26                 VoluntaryDonor = "Yes";
27             else if (rdBtnVoluntaryNo.Checked)
28                 VoluntaryDonor = "No";
29             if (rdBtn3M.Checked)
30                 DonationPeriod = "3M";
31             else if (rdBtn6M.Checked)
32                 DonationPeriod = "6M";
33             else if (rdBtn12M.Checked)
34                 DonationPeriod = "12M";


just STOP. You must know in the back of your mind that this can't be the way experienced coders do things. There must be a part of your thinking that asks "Is there a better way?" When that happens stop and find it, if only as a way to improve the quality of your code and education.

What should happen is you have an enum for DonorType and an enum for DonationPeriod. When a checkbox changes its .Checked status you update the value. Then you can refer to it.

public enum DonorType
{
   UNKNOWN = 0,
   Voluntary = 1,
   Replacement = 2
   Auto = 3
}


Or at the very least do something more elegant than all this if...else...if - It looks like 1980's BASIC.

DonationPeriod = rdBtn3m.Checked ? "3m" : DonationPeriod;
DonationPeriod = rdBtn6m.Checked ? "6m" : DonationPeriod;
DonationPeriod = rdBtn12m.Checked ? "12m" : DonationPeriod;

3 easy to read lines

We simply do not hard code strings like that all over the code. What happens when you realize that a string of "3m" can't be worked with mathmatically? That you just need 3... Or your boss says "Present this in number of days instead of months"... You are royally fraked because you have to scrub through hundreds of lines of code. Personally I would just record the DateTime of the last donation, then report on screen the number of days/months.

int DaysSinceLastDonation = (DateTime.Now - DateTimeOfLastDonation).Days;
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1