Data not inserting

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 13
Reputation: sid.coco is an unknown quantity at this point 
Solved Threads: 0
sid.coco sid.coco is offline Offline
Newbie Poster

Data not inserting

 
0
  #1
Mar 5th, 2009
I want to simply insert a few string values which i have successfully retrieved from an Excel Sheet. I have tried the "stored procedure" method but am not able to get what what i want. Just a simple C# code to insert value in SQL server 2005 using the SQLquery commands. Here's my code please tell me what's wrong in it

OleDbConnection ExcelCon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtFileSource.Text + ";Extended Properties=Excel 8.0");
ExcelCon.Open();
try
{
//Create Dataset and fill with imformation from the Excel Spreadsheet for easier reference
DataSet ExcelDataSet = new DataSet();
OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(" SELECT Name,Address,Contact FROM [" + txtSheetName.Text + "$]", ExcelCon);
ExcelAdapter.Fill(ExcelDataSet);
ExcelCon.Close();
txtJustCheck.Text = "DataSet Filled";

//Creating Database Connection
SqlConnection DBCon = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Students.mdf;Integrated Security=True;User Instance=True");
string qry;

//Travers through each row in the dataset
foreach (DataRow ExcelRow in ExcelDataSet.Tables[0].Rows)
{
if (ExcelRow["Name"].ToString() != "")
{
txtJustCheck.Text = ExcelRow["Name"].ToString();
//maxid = maxid + 1;
qry = "INSERT INTO StudentDetails(Name, Address, Contact, Institute, Course, Batch) VALUES('" + ExcelRow["Name"]+ "','" + ExcelRow["Address"]+ "','" + ExcelRow["Contact"]+ "','" + txtInstitute.Text + "','" + cbxCourse.Text + "','" + txtBatch.Text + "')";
DBCon.Open();
SqlCommand cmd = new SqlCommand("",DBCon);
//String SQLCommand = "BEGIN TRANSACTION\r\n";
//SQLCommand += qry;
//SQLCommand += "Commit Transaction";
cmd.CommandText = qry;
int Count = cmd.ExecuteNonQuery();
if (Count == 1)
{
txtJustCheck.Text = "Chal Raha Hai";
}
DBCon.Close();
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Data not inserting

 
0
  #2
Mar 5th, 2009
What Id suggest is look at the value of qry, check it visually for validity, if ok, then run it direct in something like msaccess or sql query builder or such, and see if you get an error, such as some value is a dup, its missing a required field, or whatever
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 111
Reputation: ChaseVoid is an unknown quantity at this point 
Solved Threads: 12
ChaseVoid's Avatar
ChaseVoid ChaseVoid is offline Offline
Junior Poster

Re: Data not inserting

 
0
  #3
Mar 5th, 2009
Have you tried to use the Update method of the Adapter Class?
  1. ...
  2. DataSet sqlDS = new DataSet("StudentDetails);
  3. adapter.Fill(sqlDS, "StudentDetails");
  4.  
  5. foreach(DataRow dr in excelDS.Tables["StudentDetails"].Rows)
  6. {
  7. sqlDS.Tables["StudentDetails"].Rows.Add(dr);
  8. }
  9.  
  10. adapter.Update(sqlDS, "StudentDetails");
  11. ...
  12.  

This will copy all data from excel to sql, if the structure of the data is the same. Of course there are better ways, but I haven't used ADO.net for a while so I'm unable to come up with a better solution.

Hope it helps. Please inform me okay.

Opps, forgot to mention about parametrized quires. They really help you know.

  1. //...
  2. String query = " SELECT * FROM StudentDetails WHERE name = @name";
  3.  
  4. SqlCommand cmd = new SqlCommand(query,con);
  5. cmd.Parameters.Add(new SqlParameter("@name", txtName.Text));
  6.  
  7. cmd.ExecuteNonQuery();
Last edited by ChaseVoid; Mar 5th, 2009 at 2:07 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC