Solved

insert into table fro, grid

Posted on 2006-11-09
2
441 Views
Last Modified: 2012-06-27
hi there ,

i have a grid that is bind to dataset by code only
when the Form is load this is the code that perfromed :

DataSet ds = new DataSet();
            SqlDataAdapter ad;
            SqlConnection Con = new SqlConnection();
            private void CourseTav_Load(object sender, System.EventArgs e)
            {
                  Con.ConnectionString=BL.Conn.ConnString();
                  ad = new SqlDataAdapter("Select * from Course",Con);
                  ad.Fill(ds);

                  gridEX1.DataSource=ds;
                  gridEX1.DataMember=ds.Tables[0].ToString();
                  gridEX1.RetrieveStructure();

                  gridEX1.RootTable.Columns.RemoveAt(0);
                  
                  gridEX1.RootTable.Columns["CourseNum"].Caption="מס' קורס";
                  gridEX1.RootTable.Columns["CourseNum"].Width=90;

                  gridEX1.RootTable.Columns["CourseName"].Caption="שם קורס";
                  gridEX1.RootTable.Columns["CourseName"].Width=160;
                              
            }
now i want to insert row in the grid and click the update buuton

this is the code of the bottun :

SqlCommand cmdIns = new SqlCommand("INSERT INTO Course (CourseNum,CourseName) VALUES  (@Cnum,@Cname)",Con);

                                          
                        SqlParameter parm = new SqlParameter("@Cnum",SqlDbType.VarChar,50);
                        parm.Direction=ParameterDirection.Input;
                        parm.SourceVersion=DataRowVersion.Current;
                  
                        SqlParameter parm1 = new SqlParameter("@Cname",SqlDbType.VarChar,50);
                        parm1.Direction=ParameterDirection.Input;
                        parm1.SourceVersion=DataRowVersion.Current;
                        
                        cmdIns.Parameters.Clear();
                        cmdIns.Parameters.Add(parm);
                        cmdIns.Parameters.Add(parm1);

                        ad.InsertCommand.Connection=Con;
                        ad.InsertCommand.CommandText=cmdIns.CommandText;
                        ad.Update(ds);
i am geeting this Exsption :

Object reference not set to an instance of an object.

what do i need to do to insert row  ?
thanks....
0
Comment
Question by:Tech_Men
2 Comments
 
LVL 12

Accepted Solution

by:
sumix earned 400 total points
ID: 17905281
You didn't create an instance for ad.InsertCommand, you need to set this like:

ad.InsertCommand = cmdIns;
     (the error appears when you try to set connection and commandtext and the InsertCommand is null)
0
 

Author Comment

by:Tech_Men
ID: 17905356
great thanks....
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now