none
I have got to Design a New SQL Database with Visual C# 2010 Express Edition..... RRS feed

  • General discussion

  • Hi Team,I'm a Student in SriLanka and I interested about Visual C# Programming.

    I have got to Design a New SQL Database with Visual C# 2010 Express Edition and When it Finished I had run it.But when It was Debugging there was an Error.I Couldn't find out the Problem. So If you can please help me as you know.....

    These are the Codes What I have already Typed....

    _____________________________________________________________________

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            System.Data.SqlClient.SqlConnection con;
            DataSet ds1;
            System.Data.SqlClient.SqlDataAdapter da;
            int MaxRows = 0;
                int inc = 0;

            private void Form1_Load(object sender, EventArgs e)
            {
                con = new System.Data.SqlClient.SqlConnection();
                ds1 = new DataSet();
                con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\MyDatabase.mdf;Integrated Security=True;User Instance=True";
                string SQL = "select*From Address";
                da = new System.Data.SqlClient.SqlDataAdapter(SQL,con);
                da.Fill(ds1,"Address");
                con.Open();
                //MessageBox.Show("Database Opened");
                Navigaterecords();
                MaxRows = ds1.Tables["Address"].Rows.Count;
                //con.Close();
                //MessageBox.Show("Database Closed");
                //con.Dispose();
            }
            private void Navigaterecords()
            {
                DataRow dRow = ds1.Tables["Address"].Rows[inc];
                textBox2.Text = dRow.ItemArray.GetValue(0).ToString();
                textBox3.Text = dRow.ItemArray.GetValue(1).ToString();
                textBox4.Text = dRow.ItemArray.GetValue(2).ToString();
                textBox5.Text = dRow.ItemArray.GetValue(4).ToString();
            }

            private void button2_Click(object sender, EventArgs e)
            {
                if (inc != MaxRows - 1)
                {
                    inc++;
                    Navigaterecords();
                }
                else
                {
                    MessageBox.Show("No More Records");
                }
            }

            private void button1_Click(object sender, EventArgs e)
            {
                if (inc > 0)
                {
                    inc--;
                    Navigaterecords();
                }
                else
                {
                    MessageBox.Show("First Record");
                }
            }

            private void button4_Click(object sender, EventArgs e)
            {
                if (inc != MaxRows - 1)
                {
                    inc = MaxRows - 1;
                    Navigaterecords();
                }
            }

            private void button3_Click(object sender, EventArgs e)
            {
                if (inc != 0)
                {
                    inc = 0;
                    Navigaterecords();
                }
            }

            private void button5_Click(object sender, EventArgs e)
            {
                textBox2.Clear();
                textBox3.Clear();
                textBox4.Clear();
                textBox5.Clear();
            }

            private void button6_Click(object sender, EventArgs e)
            {
                System.Data.SqlClient.SqlCommandBuilder cb;
                cb = new System.Data.SqlClient.SqlCommandBuilder(da);
                DataRow dRow = ds1.Tables["Address"].NewRow();
                dRow[0] = textBox2.Text;
                dRow[1] = textBox3.Text;
                dRow[2] = textBox4.Text;
                dRow[3] = textBox5.Text;

                ds1.Tables["Address"].Rows.Add(dRow);
                MaxRows = MaxRows + 1;
                inc = MaxRows - 1;
                da.Update(ds1, "Address");
                MessageBox.Show("Entry Added");
            }

            private void button7_Click(object sender, EventArgs e)
            {
                System.Data.SqlClient.SqlCommandBuilder cb;
                cb = new System.Data.SqlClient.SqlCommandBuilder(da);
                System.Data.DataRow dRow2 = ds1.Tables["Address"].Rows[inc];
                dRow2[0] = textBox2.Text;
                dRow2[1] = textBox3.Text;
                dRow2[2] = textBox4.Text;
                dRow2[3] = textBox5.Text;
                da.Update(ds1, "Address");
                MessageBox.Show("Data Updated");
            }

            private void button9_Click(object sender, EventArgs e)
            {
                string searchFor;
                int results = 0;
                DataRow[]returnedRows;
                searchFor = textBox1.Text;
                returnedRows = ds1.Tables["Address"].Select("Name ='"+searchFor + "'");
                results = returnedRows.Length;
                if (results > 0)
                {
                    DataRow dr1;
                    dr1 = returnedRows[0];
                    textBox2.Text = dr1["ICTM_NO"].ToString();
                    textBox3.Text = dr1["Name"].ToString();
                    textBox4.Text = dr1["Grade"].ToString();
                    textBox5.Text = dr1["Contact_No"].ToString();
                }
                else
                {
                    MessageBox.Show("No Such Record");
                }


            }

            private void button8_Click(object sender, EventArgs e)
            {
                System.Data.SqlClient.SqlCommandBuilder cb;
                cb = new System.Data.SqlClient.SqlCommandBuilder(da);
                ds1.Tables["Address"].Rows[inc].Delete();
                MaxRows--;
                inc=0;
                Navigaterecords();
                da.Update(ds1,"Address");
                MessageBox.Show("Record Deleted");
            }


        }
    }

    • Edited by Ishara Diss Monday, October 8, 2012 4:40 AM
    Monday, October 8, 2012 4:36 AM

All replies

  • string SQL = "select*From Address";
                da = new System.Data.SqlClient.SqlDataAdapter(SQL,con);
                da.Fill(ds1,"Address");

    Sorry as if I couldn't tell you about the place the error comes.this is the row(6th Row in the Form_Load)

    Monday, October 8, 2012 4:39 AM
  • Hi Ishara,

    Welcome to the MSDN forum.

    Could you please let me know what error message you got?

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, October 10, 2012 7:40 AM