CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2009
    Posts
    1

    Post Problem In coding.....

    Hi

    Actually I Wrote The Code Below for the Datagrid......
    >>>
    Code:
    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 ComboBoxCoumnGrid
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            SqlConnection conn;
            SqlCommand cmd;
            SqlDataAdapter sda;
            DataTable dt;
            SqlCommandBuilder scb;
            DataSet ds;
            string query;
            DataGridViewComboBoxColumn column1;
            private void Form1_Load(object sender, EventArgs e)
            {
                conn = new SqlConnection("Data Source=AKSHAY;Initial Catalog=Akshay;Persist Security Info=True;User ID=cosmic; Password=*********");
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                try
                {
                    if (dt != null)
                    {
                        dt.Clear();
                    }
                    dataGridView1.DataSource = null;
                    dataGridView1.Rows.Clear();
                    dataGridView1.Refresh();
                    query = "select * from emp";
                    SetData();
                    conn.Open();
                    sda.Fill(ds, "emp");
                    dt = ds.Tables["emp"];
                    foreach (DataColumn dc in dt.Columns)
                    {
                        if (dc.ColumnName.Equals("Dept"))
                        {
                            column1 = new DataGridViewComboBoxColumn();
                            column1.DataPropertyName = dc.ColumnName;
                            column1.HeaderText = dc.ColumnName;
                            column1.Name = dc.ColumnName;
                            column1.SortMode = DataGridViewColumnSortMode.Automatic;
                            column1.ValueType = dc.DataType;
                            dataGridView1.Columns.Add(column1);
    
                            try
                            {
                                SqlCommand cmd1 = new SqlCommand("select dept_name from dept", conn);
                                SqlDataReader sdr;
                                sdr = cmd1.ExecuteReader();
                                while (sdr.Read())
                                {
                                    column1.Items.Add((string)sdr["dept_name"]);
                                }
                            }
                            catch (Exception ee)
                            {
                                MessageBox.Show("Error in retrieving The Product Name values " + ee);
                            }
                            break;
                        }
                        DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
                        column.DataPropertyName = dc.ColumnName;
                        column.HeaderText = dc.ColumnName;
                        column.Name = dc.ColumnName;
                        column.SortMode = DataGridViewColumnSortMode.Automatic;
                        column.ValueType = dc.DataType;
                        dataGridView1.Columns.Add(column);
                    }
                }
                catch (Exception ee)
                {
                    MessageBox.Show("Error in Connection " + ee);
                }
                finally
                {
                    conn.Close();
                }
            }
            private void SetData()
            {
                cmd = new SqlCommand(query, conn);
                sda = new SqlDataAdapter(cmd);
                scb = new SqlCommandBuilder(sda);
                ds = new DataSet();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                int id;
                string pname;
    
                try
                {
                    conn.Open();
    
                    for (int i = 0; i <= dataGridView1.Rows.Count; i++)
                    {
                       id = (int) dataGridView1.CurrentRow.Cells[0].Value;
                        pname = dataGridView1.CurrentRow.Cells[1].Value.ToString();
                        MessageBox.Show("The id Is " + id);
                        MessageBox.Show("The id Is " + pname);
                        cmd = new SqlCommand("insert into emp(ecode,ename) values(" + id + ",'" + pname.ToString() + "')",conn);
                        cmd.ExecuteNonQuery();
                        //this.dataGridView1.Rows.RemoveAt(this.dataGridView1.CurrentRow.Index);
                    }
                }
                catch (Exception ee)
                {
                    MessageBox.Show("Error in Insertion Record " + ee);
                }
                finally
                {
                    conn.Close();
                }
            }
        }
    }
    This Code Gave Me Error.........& I was Not Able To Insert record into The Table
    ---------------------------

    ---------------------------
    Code:
    Error in Insertion Record System.Data.SqlClient.SqlException: INSERT failed because the following SET options have incorrect settings: 'ARITHABORT'. Verify that SET options are correct for use with indexed views and/or indexes on computed columns and/or query notifications and/or xml data type methods.
    
       at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
    
       at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
    
       at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
    
       at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
    
       at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async)
    
       at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
    
       at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
    
       at ComboBoxCoumnGrid.Form1.button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\cosmic\Desktop\ComboBoxCoumnGrid\ComboBoxCoumnGrid\ComboBoxCoumnGrid\Form1.cs:line 117

    Please Help Me....


    Regards
    Akshay
    Last edited by Shuja Ali; June 23rd, 2009 at 01:08 PM. Reason: Added Code tags

  2. #2
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Problem In coding.....

    without code tags I won't even try to read it
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured