Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have created a simple data insert system? and there is a save method. all the textbox data save in a excel sheet. one file goes to shared folder and other file save in user's pc.

so how can i write these details to two files in separate destination?

i have used two data sources at once in one oledb connection.

C#
{


            string szConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\172.21.1.91\enquiry/enquiry-Excel.xls;Data Source=d:\\enquiry1/enquiry-Excel.xls;Extended Properties='Excel 8.0;HDR=YES;'";
            OleDbConnection conn = new OleDbConnection(szConn);
            

            DateTime result = dateTimePicker1.Value;
            string date1 = result.ToString();
            String name = textBox1.Text;
            int Contct = Int32.Parse(textBox2.Text);
            DateTime result2 = dateTimePicker2.Value;
            string date2 = result2.ToString();
            string requirment = textBox3.Text;
            string cb = "";
            string cb2 = "";
            string cb3 = "";

            if (checkBox1.Checked)
            {
                cb = "checked";
            }
            else if (checkBox2.Checked)
            {
                cb2 = "checked";
            }
            else if (checkBox3.Checked)
            {
                cb3 = "checked";
            }

            string team;
            team = comboBox3.Text;

            string cmb;
            cmb = comboBox1.Text;

            string cmb2;
            cmb2 = comboBox2.Text;

            string status1 = textBox5.Text;
            string status2 = textBox6.Text;

           

            conn.Open();
            OleDbCommand cmd = new OleDbCommand("INSERT INTO [Sheet1$]([Team],[Date],[Name],[Contact Details],[Req Date],[Requirment],[Ticket],[Fit Package],[Group],[No of Pax],[Status 1],[Status 2],[Status 3]) VALUES('" + team + "','" + date1 + "','" + name + "','" + Contct + "','" + date2 + "','" + requirment + "','" + cb + "','" + cb2 + "','" + cb3 + "','" + cmb2 + "','" + cmb + "','" + status1 + "','" + status2 + "')", conn);
            OleDbCommand cmd2 = new OleDbCommand("INSERT INTO [Sheet1$]([Team],[Date],[Name],[Contact Details],[Req Date],[Requirment],[Ticket],[Fit Package],[Group],[No of Pax],[Status 1],[Status 2],[Status 3]) VALUES('" + team + "','" + date1 + "','" + name + "','" + Contct + "','" + date2 + "','" + requirment + "','" + cb + "','" + cb2 + "','" + cb3 + "','" + cmb2 + "','" + cmb + "','" + status1 + "','" + status2 + "')", conn);
            cmd.ExecuteNonQuery();
            cmd2.ExecuteNonQuery();

            conn.Close();
            MessageBox.Show("complete");     
        }
Posted
Updated 31-Oct-14 17:49pm
v2
Comments
Robert Welliever 1-Nov-14 0:16am    
You should use the OleDbParameter object. I guess it's dependent on who is accessing the code, but it really would be easy for a user to delete all the data from that file by injecting a Delete statement into your textbox.

1 solution

use two connection strings with the two different locations. assume szConn1 and szConn2

C#
OleDbConnection conn1 = new OleDbConnection(szConn1);
OleDbConnection conn2 = new OleDbConnection(szConn2);

OleDbCommand cmd1 = new OleDbCommand(sqlString, conn1); 
OleDbCommand cmd2 = new OleDbCommand(sqlString, conn2); 

conn1.Open();
conn2.Open();

cmd1.ExecuteNonQuery();
cmd2.ExecuteNonQuery();


after you successfully write in to two files you better study about how to use sql parameters and also use of using statements. Sample code:

C#
string SqlString = "Insert Into Contacts (FirstName, LastName) Values (?,?)";
using (OleDbConnection conn = new OleDbConnection(ConnString))
{
  using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
  {
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.AddWithValue("FirstName", txtFirstName.Text);
    cmd.Parameters.AddWithValue("LastName", txtLastName.Text);
    conn.Open();
    cmd.ExecuteNonQuery();
  }
}
 
Share this answer
 
v2
Comments
Robert Welliever 1-Nov-14 0:11am    
Why are you leaving the connection open?
DamithSL 1-Nov-14 0:19am    
I haven't add entire code, only wanted to show how to use two connection strings. Anyway I have updated answer with sample code with parameters and using blocks.
Hemas Ilearn 1-Nov-14 0:25am    
i did like this?

string szConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\172.21.1.91\enquiry/enquiry-Excel.xls;Extended Properties='Excel 8.0;HDR=YES;'";
string szConn1 = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\\enquiry1/enquiry-Excel.xls;Extended Properties='Excel 8.0;HDR=YES;'";
OleDbConnection conn = new OleDbConnection(szConn);
OleDbConnection conn1 = new OleDbConnection(szConn1);

OleDbCommand cmd = new OleDbCommand("INSERT INTO [Sheet1$]([Team],[Date],[Name],[Contact Details],[Req Date],[Requirment],[Ticket],[Fit Package],[Group],[No of Pax],[Status 1],[Status 2],[Status 3]) VALUES('" + team + "','" + date1 + "','" + name + "','" + Contct + "','" + date2 + "','" + requirment + "','" + cb + "','" + cb2 + "','" + cb3 + "','" + cmb2 + "','" + cmb + "','" + status1 + "','" + status2 + "')", conn);
OleDbCommand cmd2 = new OleDbCommand("INSERT INTO [Sheet1$]([Team],[Date],[Name],[Contact Details],[Req Date],[Requirment],[Ticket],[Fit Package],[Group],[No of Pax],[Status 1],[Status 2],[Status 3]) VALUES('" + team + "','" + date1 + "','" + name + "','" + Contct + "','" + date2 + "','" + requirment + "','" + cb + "','" + cb2 + "','" + cb3 + "','" + cmb2 + "','" + cmb + "','" + status1 + "','" + status2 + "')", conn);

conn.Open();
conn1.Open();

cmd.ExecuteNonQuery();
cmd2.ExecuteNonQuery();

conn.Close();
MessageBox.Show("complete");
conn1.Close();

but still have same problem?
DamithSL 1-Nov-14 0:28am    
bath cmd and cmd2 using conn! change cmd2 initialization with conn1

OleDbCommand cmd2 = new OleDbCommand("INSERT INTO [Sheet1$]([Team],[Date],[Name],[Contact Details],[Req Date],[Requirment],[Ticket],[Fit Package],[Group],[No of Pax],[Status 1],[Status 2],[Status 3]) VALUES('" + team + "','" + date1 + "','" + name + "','" + Contct + "','" + date2 + "','" + requirment + "','" + cb + "','" + cb2 + "','" + cb3 + "','" + cmb2 + "','" + cmb + "','" + status1 + "','" + status2 + "')", conn1);
Hemas Ilearn 1-Nov-14 0:39am    
got it.... (y)
thank you

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