Solved

insert into table fro, grid

Posted on 2006-11-09
2
440 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

16 Experts available now in Live!

Get 1:1 Help Now