Click here to Skip to main content
15,879,096 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here my problem is how to inset data here my code

DAL:
C#
#region InsertData
   public int Insert(NCBAL ObjInsert)
   {
       con = new SqlConnection(constr);
       con.Open();
       cmd = new SqlCommand("InsertNewComp_SP", con);
       cmd.CommandType = CommandType.StoredProcedure;
       try
       {
           cmd.Parameters.AddWithValue("@compliantID",ObjInsert.compid).ToString();
           cmd.Parameters.AddWithValue("@customerid", ObjInsert.cusid).ToString();
           cmd.Parameters.AddWithValue("@customername", ObjInsert.cusname).ToString();
           cmd.Parameters.AddWithValue("@branchid", ObjInsert.ddlbranch).ToString();
           cmd.Parameters.AddWithValue("@servicetype",ObjInsert.servicetype);
           return cmd.ExecuteNonQuery();
       }
       catch
       {
           throw;
       }
       finally
       {
           cmd.Dispose();
           con.Close();
           con.Dispose();
       }
   }
   #endregion


BAL:

C#
#region Insert Data
   public int Insert(NCBAL ObjInsert)
   {
       try
       {
           return ncd.Insert(ObjInsert);
       }
       catch
       {
           throw;
       }
       finally
       {
           ncd = null;
       }
   }
   #endregion


UI:

C#
ncb.compid = txtcompid.Text;
                        ncb.cusid = txtcusid.Text;
                        ncb.cusname = txtcusname.Text;
                        ncb.cusnum = txtcusnumber.Text;
                        ncb.compname = txtcompname.Text;
                        ncb.ddlproduct = DDlProductname.Text;
                        ncb.compliant = txtCompliant.Text;
                        ncb.compdate = Convert.ToDateTime(txtcompdate.Text);
                        ncb.solution = txtsolution.Text;
                        ncb.ddlassign = DDlAssign.Text;
                        ncb.datetocomp = Convert.ToDateTime(txtdatetocom.Text);
                        ncb.ddlpriorty = DDlPriorty.Text;
                        ncb.ddlstatus = DDlStatus.Text;
                        ncb.complianttime = txtcomplianttime.Text;
                        ncb.datetocomp = txtdatecomptime.Text;
                        ncb.ddlbranch = DDLbranch.Text;
                        String str = "";
                        for (int i = 0; i <= CBLservicetype.Items.Count - 1; i++)
                        {
                            if (CBLservicetype.Items[i].Selected)
                            {
                                if (str == "")
                                {
                                    str = CBLservicetype.Items[i].Text;
                                }
                                else
                                {
                                    str += "," + CBLservicetype.Items[i].Text;
                                }
                            }
                        }
                        ncb.servicetype = str;
                    }


the data not saved in database and another things is i want to check (result>0) how to do that...
Posted
Updated 14-May-14 23:42pm
v2
Comments
_Maxxx_ 15-May-14 0:43am    
Is the SP running at all?
Is the DAL method run at all?
Is the BAL method run at all?

Why have the catch .. throw - leave the exceptions in while you are testing and it may give you a clue!

If you want the SP to return the number of rows affected then you need to write the SP to do so.
prasanna.raj 15-May-14 0:49am    
is there any possible to store the result in (int) then i can check that it possible...
_Maxxx_ 15-May-14 2:05am    
The result of what? The query?
Something like this http://stackoverflow.com/questions/1501635/how-to-get-the-return-value-and-rows-in-c-sharp-thru-stored-procedure
If it were me I would:
Run in debug, up to the call on the SP - if it doesn't get there then you need to dig in your code
If it does, then call the SP through SSMS (or even just run the sql) with the same values you're assigning in the program.
See what happens!
Jignesh Khant 15-May-14 1:42am    
Your DAL function should return some integer value.
syed shanu 15-May-14 1:57am    
Where do you call your Insert function in UI.i cannot see any insert call function of your BAL Insert function.

1 solution

C#
BAL  bal = new BAL();
int returnVal = bal.Insert(ncb);
if( returnVal > 0)
   //Insert Success
else
  //Insert Failed
 
Share this answer
 
Comments
prasanna.raj 15-May-14 6:07am    
Thank You Sir....
Sunasara Imdadhusen 15-May-14 6:09am    
Happy to help you. does above code work for you?
prasanna.raj 15-May-14 6:26am    
yes it work well...
prasanna.raj 15-May-14 6:30am    
sir i have small doubt why we create object for
BAL bal=new BAL();
i already create BAL ncb =new BAL();
what is the reason...
Sunasara Imdadhusen 15-May-14 6:49am    
ok then you don't want to create new instance and more thing that you are passing object in Insert method that should not required. because object (ncb) itself properties value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900