Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

insert into table fro, grid

Posted on 2006-11-09
2
Medium Priority
?
456 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 12

Accepted Solution

by:
sumix earned 1600 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

636 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