Solved

insert into table fro, grid

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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