problem ado.net application

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mo/-/sin

    problem ado.net application

    hi. i m mohsin
    i m making a gui application using c# at front end and sql server 2005
    at backend...... i made a table in sql server 2005 with 4 columns and
    in gui i used 4 text boxes and 4labels and three buttons. my full code
    is mentioned below....... button1 is for add button2 is for save and
    button3 is for delete...
    the table is empty..but the lines prefix with ---- are giving
    errors......... ... plz tel me why............ i also used a binding
    navigator and bind all the columns correctly...... .......



    using System;
    using System.Collecti ons.Generic;
    using System.Componen tModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows. Forms;
    using System.Data.Sql Client;

    namespace CITY_HOSPITAL
    {

    public partial class Form20 : Form
    {
    DataTable dt;
    DataRow dr;
    string code;
    public Form20()
    {
    InitializeCompo nent();

    }

    private void textBox3_TextCh anged(object sender, EventArgs e)
    {

    }

    private void Form20_Load(obj ect sender, EventArgs e)
    {
    // TODO: This line of code loads data into the
    'cITY_HOSPITALD ataSet.SERVICES MAINTENANCE' table. You can move, or
    remove it, as needed.

    this.sERVICESMA INTENANCETableA dapter.Fill(thi s.cITY_HOSPITAL DataSet.SERVICE SMAINTENANCE);
    textBox1.Enable d = false;
    textBox2.Enable d = false;
    textBox3.Enable d = false;
    textBox4.Enable d = false;
    button2.Enabled = false;
    }

    private void button1_Click(o bject sender, EventArgs e)
    {
    button2.Enabled = true;
    textBox2.Enable d = true;
    textBox3.Enable d = true;
    textBox4.Enable d = true;
    textBox2.Text = "";
    textBox3.Text = "";
    textBox4.Text = "";
    int ctr, len;
    string codeval;
    dt = cITY_HOSPITALDa taSet.Tables["SERVICESMAINTE NANCE"];
    ----- len = dt.Rows.Count - 1;
    ---- dr = dt.Rows[len];
    code = dr["SERVICEID"].ToString();
    codeval = code.Substring( 1, 5);
    ctr = Convert.ToInt32 (codeval);
    if ((ctr >= 1) && (ctr < 9))
    {
    ctr = ctr + 1;
    textBox1.Text = "SER00" + ctr;
    }
    else if ((ctr >= 9) && (ctr < 99))
    {
    ctr = ctr + 1;
    textBox1.Text = "SER0" + ctr;
    }
    else if (ctr >= 99)
    {
    ctr = ctr + 1;
    textBox1.Text = "SER" + ctr;
    }
    button1.Enabled = false;
    }

    private void button2_Click(o bject sender, EventArgs e)
    {
    dt = cITY_HOSPITALDa taSet.Tables["SERVICESMAINTE NANCE"];
    dr = dt.NewRow();
    dr[0] = textBox1.Text;
    dr[1] = textBox2.Text;
    dr[2] = textBox3.Text;
    dr[3] = textBox3.Text;
    dt.Rows.Add(dr) ;

    sERVICESMAINTEN ANCETableAdapte r.Update(cITY_H OSPITALDataSet) ;
    textBox2.Text = System.Convert. ToString(dr[0]);
    textBox1.Enable d = false;
    textBox2.Enable d = false;
    textBox3.Enable d = false;
    textBox4.Enable d = false;

    this.sERVICESMA INTENANCETableA dapter.Fill(thi s.cITY_HOSPITAL DataSet.SERVICE SMAINTENANCE);
    button1.Enabled = true;
    button2.Enabled = false;
    }

    private void button3_Click(o bject sender, EventArgs e)
    {
    string code;
    code = textBox1.Text;
    if (textBox1.Text. Length == 0)
    {
    MessageBox.Show ("PLEASE SELECT A VALID RECORD");
    }
    else
    {
    dr =
    cITY_HOSPITALDa taSet.Tables["SERVICESMAINTE NANCE"].Rows.Find(code );
    dr.Delete();

    sERVICESMAINTEN ANCETableAdapte r.Update(cITY_H OSPITALDataSet) ;
    }

    }

    }

    }

  • Marc Gravell

    #2
    Re: problem ado.net application

    but the lines prefix with ---- are giving errors

    What errors? (please copy the exact error message)

    The code you posted gives an idea, but we can't use that "as is" to
    investigate anything. But if you give us the error message we might be
    able to help.

    Can I guess "NullReferenceE xception", "Object reference not set to an
    instance of an object."?

    In which case either the table isn't called "SERVICESMAINTE NANCE"
    inside the DataSet (just enumerate the .Tables to see what *is*
    there), or you haven't loaded it correctly. Try using the table as:
    dt = cITY_HOSPITALDa taSet.SERVICE­S MAINTENANCE;

    Or simply check that your DataSet is not completely empty.

    Marc

    Comment

    • mo/-/sin

      #3
      Re: problem ado.net application

      On Aug 19, 12:59 pm, Marc Gravell <marc.grav...@g mail.comwrote:
      but the lines prefix with ---- are giving errors
      >
      What errors? (please copy the exact error message)
      >
      The code you posted gives an idea, but we can't use that "as is" to
      investigate anything. But if you give us the error message we might be
      able to help.
      >
      Can I guess "NullReferenceE xception", "Object reference not set to an
      instance of an object."?
      >
      In which case either the table isn't called "SERVICESMAINTE NANCE"
      inside the DataSet (just enumerate the .Tables to see what *is*
      there), or you haven't loaded it correctly. Try using the table as:
      dt = cITY_HOSPITALDa taSet.SERVICE­S MAINTENANCE;
      >
      Or simply check that your DataSet is not completely empty.
      >
      Marc
      ok buddy.......
      the error is "there is no row at position -1" however if i change to 0
      or +1 the error is there is no row at position 0 or +1 respectively... .

      Comment

      • mo/-/sin

        #4
        Re: problem ado.net application

        On Aug 19, 6:14 pm, "mo/-/sin" <Ri.moh...@gmai l.comwrote:
        On Aug 19, 12:59 pm, Marc Gravell <marc.grav...@g mail.comwrote:
        >
        >
        >
        but the lines prefix with ---- are giving errors
        >
        What errors? (please copy the exact error message)
        >
        The code you posted gives an idea, but we can't use that "as is" to
        investigate anything. But if you give us the error message we might be
        able to help.
        >
        Can I guess "NullReferenceE xception", "Object reference not set to an
        instance of an object."?
        >
        In which case either the table isn't called "SERVICESMAINTE NANCE"
        inside the DataSet (just enumerate the .Tables to see what *is*
        there), or you haven't loaded it correctly. Try using the table as:
        dt = cITY_HOSPITALDa taSet.SERVICE­S MAINTENANCE;
        >
        Or simply check that your DataSet is not completely empty.
        >
        Marc
        >

        ok buddy.......
        the error is"INDEXOUTOFRA NGEEXCEPTION" below this it is written that
        "there is no row at position -1" however if i change to 0
        or +1 the error is there is no row at position 0 or +1 respectively...

        Comment

        • mo/-/sin

          #5
          Re: problem ado.net application

          On Aug 19, 6:23 pm, "mo/-/sin" <Ri.moh...@gmai l.comwrote:
          On Aug 19, 6:14 pm, "mo/-/sin" <Ri.moh...@gmai l.comwrote:
          >
          >
          >
          On Aug 19, 12:59 pm, Marc Gravell <marc.grav...@g mail.comwrote:
          >
          but the lines prefix with ---- are giving errors
          >
          What errors? (please copy the exact error message)
          >
          The code you posted gives an idea, but we can't use that "as is" to
          investigate anything. But if you give us the error message we might be
          able to help.
          >
          Can I guess "NullReferenceE xception", "Object reference not set to an
          instance of an object."?
          >
          In which case either the table isn't called "SERVICESMAINTE NANCE"
          inside the DataSet (just enumerate the .Tables to see what *is*
          there), or you haven't loaded it correctly. Try using the table as:
          dt = cITY_HOSPITALDa taSet.SERVICE­S MAINTENANCE;
          >
          Or simply check that your DataSet is not completely empty.
          >
          Marc
          >
          ok buddy.......
          the error is"INDEXOUTOFRA NGEEXCEPTION" below this it is written that
          "there is no row at position -1" however if i change to 0
          or +1 the error is there is no row at position 0 or +1 respectively...
          as per your suggestion i insert two rows directly into that table
          using sql server 2005... but now the error is in the line " ctr =
          Convert.ToInt32 (codeval); " and the error is formatexception was
          unhandled and below that it is written that INPUT STRING WAS NOT IN A
          CORRECT FORMAT'........ .....

          Comment

          • Marc Gravell

            #6
            Re: problem ado.net application

            OK, so the original error was that there weren't any rows and you
            weren't checking for this...

            The second error; what is a typical "codeval" value when you debug it?

            Marc

            Comment

            • mo/-/sin

              #7
              Re: problem ado.net application

              On Aug 19, 7:40 pm, Marc Gravell <marc.grav...@g mail.comwrote:
              OK, so the original error was that there weren't any rows and you
              weren't checking for this...
              >
              The second error; what is a typical "codeval" value when you debug it?
              >
              Marc
              as i told u there are three buttons in my application and when i debug
              that application i m not able see the application instead of that i
              see this error.......... ...

              Comment

              • Marc Gravell

                #8
                Re: problem ado.net application

                as i told u there are three buttons in my application and when i debug
                that application  i m not able see the application instead of that i
                see this error.......... ...
                There are lots of ways of getting a value if the debugger isn't happy
                - write the value to the console, the trace, the event log or just a
                flat file if you need to! You're going to have to know what it thinks
                that value is to debug it.

                Marc

                Comment

                Working...