.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ADO.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Data is not retrieved from a data table

Posted By: archana     Posted Date: January 22, 2010    Points:2   Category :ADO.Net
I need to insert values into a table. The above is a code in DA layer. i am not able to pass values to parameters from the table. but i not able to retrieve the values from the table. so please kindly help. Below is my code

public int Insert(List<EmployeeUInfo> objInsert)
{
try
{


//DataSet ds = new DataSet();
con.Open();

//MySqlCommand cmd = new MySqlCommand("insert into employee(employeeid,employeename,business,emailid,phoneno) values(" +obj.employeeid + ",'" + obj.employeename + "','" + obj.business + "','" + obj.emailid + "'," + obj.phoneno + ")", con);
//cmd.CommandType = CommandType.Text;
//return cmd.ExecuteNonQuery();


DataTable dtInsertRow = new DataTable();

DataColumn dc = new DataColumn();
dtInsertRow.Columns.Add("EmployeeId",typeof(Int32));
dc = new DataColumn();
dtInsertRow.Columns.Add("EmployeeName",typeof(string));
dc = new DataColumn();
dtInsertRow.Columns.Add("Bussiness",typeof(string));
dc = new DataColumn();
dtInsertRow.Columns.Add("Email",typeof(string));
dc = new DataColumn();
dtInsertRow.Columns.Add("Phoneno",typeof(Int32));

foreach (EmployeeUInfo objIDetails in objInsert)
{
DataRow dr = dtInsertRow.NewRow();
dtInsertRow.Rows.Add(objIDetails.employeeid, objIDetails.employeename,objIDetails.business, objIDetails.emailid, objIDetails.phoneno);
}
MySqlCommand cmd = new MySqlCommand("insert into employee values(@EmployeeId,@EmployeeName,@Bussiness,@Email,@Phoneno)", con);
cmd.CommandType = CommandType.Text;

cmd.Parameters.Add("@EmployeeId", SqlDbType.Int).Value = Convert.ToInt32(dtInsertRow.Rows[0]["EmployeeId"]);
cmd.Parameters.Add("@EmployeeName", SqlDbType.VarChar).Value = Convert.ToString(dtInsertRow.Rows[0]["EmployeeName"]);
cmd.Parameters.Add("@Bussiness", SqlDbType.VarChar).Value = Convert.ToString(dtInsertRow.Rows[0]["Bussiness"]);
cmd.Parameters.Add("@Email", SqlDbType.VarChar).Value = Convert.ToString(dtInsertRow.Rows[0]["Email"]);
cmd.Parameters.Add("@Phoneno", SqlDbType.Int).Value = Convert.ToInt32(dtInsertRow.Rows[0]["Phoneno"]);
MySqlDataAdapter adpt = new MySqlDataAdapter();
adpt.InsertCommand = cmd;
//adpt.UpdateBatchSize = objInsert.Count;
//int recordsinserted = adpt.Update(dtInsertRow);
return cmd.ExecuteNonQuery();

}
catch
{

throw;

}

finally
{

con.Close();
con.Dispose();
}


}


need to insert values into a table. The above is a code in DA layer. i am not able to pass values to parameters from the table. but i not able to retrieve the values from the table. so please kindly help

Responses
Author: AK             
Posted Date: January 22, 2010     Points: 5   

While debugging is there any data in your datatable?

Author: archana             
Posted Date: January 22, 2010     Points: 5   

ya there is data in the datatable.....i am able to retrieve the data now buts its not being inserted

Author: AK             
Posted Date: January 22, 2010     Points: 5   

Please debug the application and check whether you get values in these lines

cmd.Parameters.Add("@EmployeeId", SqlDbType.Int).Value = Convert.ToInt32(dtInsertRow.Rows[0]["EmployeeId"]);
cmd.Parameters.Add("@EmployeeName", SqlDbType.VarChar).Value = Convert.ToString(dtInsertRow.Rows[0]["EmployeeName"]);
cmd.Parameters.Add("@Bussiness", SqlDbType.VarChar).Value = Convert.ToString(dtInsertRow.Rows[0]["Bussiness"]);
cmd.Parameters.Add("@Email", SqlDbType.VarChar).Value = Convert.ToString(dtInsertRow.Rows[0]["Email"]);
cmd.Parameters.Add("@Phoneno", SqlDbType.Int).Value = Convert.ToInt32(dtInsertRow.Rows[0]["Phoneno"]);

And check the values in
return cmd.ExecuteNonQuery();

Try like int a = cmd.ExecuteNonQuery();

And check what is the value of a
if its greater than zero it will insert archana..

Author: archana             
Posted Date: January 22, 2010     Points: 5   

if i use cmd.executenonquery ist giving me an error that employee id cannot be null. But i can see the values in the parameters while debugging

Author: AK             
Posted Date: January 22, 2010     Points: 5   

Archana then may ne the error in the following line

cmd.Parameters.Add("@EmployeeId", SqlDbType.Int).Value = Convert.ToInt32(dtInsertRow.Rows[0]["EmployeeId"]);

Author: archana             
Posted Date: January 22, 2010     Points: 5   

may be
then what is the solution

Author: AK             
Posted Date: January 22, 2010     Points: 5   

The employee value canot be null means you are entering a null value.I mean the compliler not getting the value from your datatable i think.Check whether you specify the correct fieldnames and try it out in a small program and check there you can insert it correctly.Sorry to say that i cant able to access the copied code.so u please check it out with a small program.And If you got the output post the error here so that the readers let know it.



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend