Solved

insert into table fro, grid

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

707 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