I have been working on a project related to database (.mdf). I have created some windows forms in visual studio using C#. Basically these forms work together to store, update and delete data from the Service Based Database i created.
The problem is when i build the project, it builds fine, no errors. It inserts a data provided from textboxes to the datagridview too as intended. But as soon as i stop the current debugging, and then rerun it again, all the data provided previously is lost from the datagridview!!
I cant understand why this is happening. anyone please help me. Im totally new to this stuff.. a bit of guidance would be heartily appreciated.
when i had previously used MySQL for the same purpose, the updated data would be permanently stored to the database, but since i migrated from the MySQL to SQL Server's Service Based Database, i get such confusing error.
......
void loadData()
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["baikalpik_bidhut_sewaConnectionString"].ToString());
SqlCommand cmd = new SqlCommand("SELECT SNo,Customer_ID, Citizenship_No, Name, Subscription_Date, Phone_No, Location,Locality,Bulbs,Deposit,Monthly_Charge FROM customerinformation;", con);
try
{
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
DataTable dt = new DataTable();
adp.Fill(dt);
dataGridViewCustomerInformation.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void buttonAdd_Click(object sender, EventArgs e)
{
try
{
float m_chrg = Convert.ToInt64(textBoxBulbs.Text)*500;
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["baikalpik_bidhut_sewaConnectionString"].ToString());
SqlCommand cmd = new SqlCommand("INSERT INTO customerinformation(SNo,Customer_ID,Citizenship_No,Name,Subscription_Date,Location,Locality,Bulbs,Deposit,Phone_No,Monthly_Charge) values('" + textBoxSNo.Text + "','" + textBoxCustomerID.Text + "','" + textBoxCitizenshipNumber.Text + "','" + textBoxName.Text + "','" + textBoxSubscriptionDate.Text + "','" + textBoxLocation.Text + "','" + textBoxLocality.Text + "','" + textBoxBulbs.Text + "','" + textBoxDeposit.Text + "','" + textBoxPhoneNumber.Text + "','" + m_chrg + "')", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
dt = new DataTable();
dt.Load(reader);
con.Close();
dataGridViewCustomerInformation.DataSource = dt;
loadData();
MessageBox.Show("Entry Added!");
fillListbox();
textBoxSNo.Clear();
textBoxBulbs.Clear();
textBoxCitizenshipNumber.Clear();
textBoxCustomerID.Clear();
textBoxDeposit.Clear();
textBoxLocality.Clear();
textBoxLocation.Clear();
textBoxPhoneNumber.Clear();
textBoxName.Clear();
textBoxSubscriptionDate.Clear();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
If you have your MDF listed in your project files then the property Copy To Output Directory handles how your MDF file is copied to the output directory (BIN\DEBUG or BIN\RELEASE).
If this property is set to Copy Always, then when you run your program a fresh copy of your database file is copied from the project folder to the output effectively destroying every data that you have inserted, modified, deleted in the previous run of your program.
The best solution to this dilemma is to add the MDF file as a permanent database inside your current install of Sql Server and adjust your connection string. And of course set the property to Copy Never
In alternative you could set to Copy if newer. This will allow to change the database schema inside the Server Explorer and let the Visual Studio IDE copy the new structure only after you have made changes.
UPDATE BUT IMPORTANT Not related to your actual question, but your insert query is a serious problem. Do not use string concatenation to build a sql command text. Particularly if the partial text comes from user input. You could face syntax errors (if someone places a single quote inside a text box) or worst, a Sql Injection problem (see here for a funny explanation)
To find good examples of Insert search for 'parametrized query'
Related
I'm trying to add new record to trans_daily table, however, the code snippet below executed on button click doesn't work.
THE PROBLEMATIC CODE
private void button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection connection = new SqlConnection(conString);
connection.Open();
string strcom = "INSERT INTO trans_daily (retail_id, cust_name, quantity, " +
"price, date, visibility, remarks_id) VALUES (#RetailID, #CustomerName, " +
"#Quantity, #Price, #Date, #Visibility, #RemarksID)";
SqlCommand cmd = new SqlCommand(strcom, connection);
cmd.Parameters.AddWithValue("#RetailID", ddRetailType.SelectedValue);
cmd.Parameters.AddWithValue("#CustomerName", tbCustomer.Text);
cmd.Parameters.AddWithValue("#Quantity", float.Parse(tbQuantity.Text));
cmd.Parameters.AddWithValue("#Price", float.Parse(tbPrice.Text));
cmd.Parameters.AddWithValue("#Date", DateTime.Now);
cmd.Parameters.AddWithValue("#Visibility", 1);
cmd.Parameters.AddWithValue("#RemarksID", 1);
cmd.ExecuteNonQuery();
}
catch (Exception err)
{
Console.WriteLine(err.Message);
}
}
The following are the data types:
retail_id (int)
cust_name (varchar50)
quantity (float)
price (float)
date (datetime)
visibility (int)
remarks_id (int)
It's also worth to point out that no exception is being thrown.
What could have gone wrong?
THE WORKING CODE
In a separate function, I was able to pull out data from retail table on form load and placed the pulled out data to a dropdown list.
private void formAddTransaction_Load(object sender, EventArgs e)
{
try
{
objConnect = new DatabaseConnection();
conString = Properties.Settings.Default.MVGasConnectionString;
objConnect.Sql = "SELECT * FROM retail";
objConnect.connection_string = conString;
ds = objConnect.GetConnection;
MaxRows = ds.Tables[0].Rows.Count;
FillRetailTypes(conString);
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
public void FillRetailTypes(string constring)
{
ddRetailType.DataSource = ds.Tables[0];
ddRetailType.ValueMember = "id";
ddRetailType.DisplayMember = "type";
}
Try this code and you may find your error:
try
{
using(SqlConnection connection = new SqlConnection(conString))
using(SqlCommand cmd = new SqlCommand())
{
connection.Open();
string strcom = "INSERT INTO trans_daily ([retail_id], [cust_name], [quantity], " +
"[price], [date], [visibility], [remarks_id]) VALUES (#RetailID, #CustomerName, " +
"#Quantity, #Price, #Date, #Visibility, #RemarksID)";
cmd.CommandText = strcom;
cmd.Connection = connection;
cmd.Parameters.AddWithValue("#RetailID", SqlDbType.Int);
cmd.Parameter["#RetailID"].Value = ddRetailType.SelectedValue;
cmd.Parameters.AddWithValue("#CustomerName", SqlDbType.VarChar);
cmd.Parameter["#CustomerName"].Value = tbCustomer.Text;
cmd.Parameters.AddWithValue("#Quantity", SqlDbType.Float);
cmd.Parameter["#Quantity"].Value = float.Parse(tbQuantity.Text);
cmd.Parameters.AddWithValue("#Price", SqlDbType.Float);
cmd.Paramter["#Price"].Value = float.Parse(tbPrice.Text);
cmd.Parameters.AddWithValue("#Date", SqlDbType.DateTime);
cmd.Parameter["#Date"].Value = DateTime.Now;
cmd.Parameters.AddWithValue("#Visibility", SqlDbType.Int);
cmd.Parameter["#Visibility"].Value = 1;
cmd.Parameters.AddWithValue("#RemarksID", SqlDbType.Int);
cmd.Parameter["#RemarksID"].Value = 1;
int affectedRows = cmd.ExecuteNonQuery();
Console.WriteLine("You have inserted {0} rows", affectedRows);
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
This code will...
... automatically open and close your database connection and command object
... log the amount of affected rows in the database to the console
... log exception messages to the console. You may add a breakpoint to this line if you want to see the whole exception (if one occures)
As explained in this blog, it seems like the problem is in the connection string.
Inside the project folder, 2 .mdf files were created: one is found at the root of the folder (let's call it mdf_1) and the other is inside the bin\Debug folder (let's call it mdf_2).
Now, taking a look inside the Visual Studio (VS) editor, there are 2 .mdf files shown: one under the Server Explorer, and another under the Solution Explorer. Not taking note of the location (from the properties section) of these .mdf files made me think these 2 .mdf files were the same. The thing is, the one under the Server Explorer is mdf_1 while mdf_2 is the one under the Solution Explorer.
VS has a behavior that copies mdf_1 and overwrites mdf_2 everytime the program is run (because by default, .mdf is set to Copy always mdf_1 to mdf_2). My connection string was pointing to the mdf_2 which explains why despite cmd.ExecuteNonQuery() command returns 1, my successfully inserted records are always erased because of the behavior VS has. And since it is mdf_1 that is under Server Explorer, I could not verify the changes in DB. Simply put, I was making changes in mdf_2 while trying to look for the changes in mdf_1.
So to avoid this, I modified my connection string by changing the relative path |DataDirectory| in the connection string to the absolute path of mdf_1 so that no matter how often VS overwrites mdf_2, the changes I make during runtime in mdf_1 aren't overwritten. This was, changes are also reflected in the Server Explorer (where I verify if changes were actually made).
tl;dr : I was updating the .mdf file under bin\Debug folder during runtime while physically looking for the updates in the .mdf file located in the root of the project.
I'm uploading PDF files into a folder using FileUpload control in this way:
string pdfFilPath = Path.GetFileName(FileUpload1.PostedFile.FileName.ToString());
string pdfPath = Server.MapPath(#"~/PDF/" + pdfFilPath);
FileUploadFoto.PostedFile.SaveAs(pdfPath);
But at the same time I'm inserting an Id, Description and PDFUrl in the database for this file:
SqlCommand cmd = new SqlCommand("INSERT INTO Book(Description, PDFUrl)
VALUES (''' + textBoxDescription.Text + "','" + "~/PDF/" + pdfFilPath + "')", conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
If I select all columns from the database and show them on a GridView, they appear as follows:
ID Description PDFUrl
1 In this book... ~/PDF/jQuery in Action.pdf
Now, if I want to delete a book review, all I do is ("DELETE FROM Book Where Id='" + textBoxId.Text + "'", conn);, but this will delete only the Id,Description and the PDFUrl from the database.
My question is: How can I delete the PDF file at the same time when I delete the review from the database?
Don't forget though, you should check to see if the file exists before you delete it or else it will throw an error. This can happen if the user refreshes the postback.
if (File.Exists(Server.MapPath(PDFUrl))
{
File.Delete(Server.MapPath(PdfUrl));
}
If all you have is the ID, then first fun a query to get the PDFUrl from the DB based on the ID.
Then:
FileInfo fi = new FileInfo(Server.MapPath(PDFUrl));
fi.Delete;
Source: MSDN
Also, you should really use parameterized queries for reasons explained on the OWASP SQL Injection Prevention Cheat Sheet.
You can just create a method which delete the file from the directory, after that you delete the file from the database.
public void DeleteFile(string id)
{
FileInfo fi = new FileInfo(Server.MapPath(PDFUrl));
fi.Delete;
SqlComand cmd = new SqlComand("DELETE FROM Book Where Id='" + textBoxId.Text + "'", conn);
cmd.ExecuteNonQuery();
}
I have a WebService that updates my access table from some terminals (10).
When I try to update I get this error from the error log:
Could not Update; Currently locked
Some terminals succeed and some do not.
I update like this:
using (Conn = new OleDbConnection(Work_Connect))
{
Conn.Open();
foreach (DataRow R in ds.Tables["MyCount"].Rows)
{
U_ID = ID;
U_Bar = R["Bar"].ToString().Trim();
U_Qty = R["Qty"].ToString().Trim();
U_Des = R["Des"].ToString().Trim();
SQL = "INSERT INTO MyTbl(ID,Bar,Qty,Des)VALUES('";
SQL += Convert.ToInt32(ID) + "','" + U_Bar + "','" + Convert.ToDouble(U_Qty) + "','" + U_Des + "')";
OleDbCommand Cmd2 = new OleDbCommand(SQL, Conn);
Cmd2.CommandText = SQL;
Cmd2.ExecuteNonQuery();
}
}
GC.Collect();
return true;
MsAccess has serious drawbacks for multi-user update. The Jet engine is not a database server, and will manage concurrence based on file system locking. If your problem is with a web service, I'd move the update to the server part, and implement queuing of simultaneous requests there. Thus, only the server, one process, will have access to the Access data. The other option is to use a real database server that will do that work for you. SQL Server Express is the usual option because it's easy to integrate, it's free as in beer, and is solid.
Also, if your problem happens always from the same terminals, that is, some terminals can never update anything, check the file access rights of these terminals' users to the database file, the lock file, and the database and lock file directory. Write rights are required for all of them.
Suggestions:
Convert your query to a parameterized query to avoid any potential strangeness with quoting. (You are converting text to numbers and then enclosing them in single-quotes in the SQL statement. That makes no sense.)
Don't force garbage collection on each call. According to the MSDN article here: "It is possible to force garbage collection by calling Collect, but most of the time, this should be avoided because it may create performance issues."
Try something like this instead:
using (Conn = new OleDbConnection(Work_Connect))
{
Conn.Open();
foreach (DataRow R in ds.Tables["MyCount"].Rows)
{
U_ID = ID;
U_Bar = R["Bar"].ToString().Trim();
U_Qty = R["Qty"].ToString().Trim();
U_Des = R["Des"].ToString().Trim();
SQL = "INSERT INTO MyTbl (ID,Bar,Qty,Des) VALUES (?,?,?,?)";
using(OleDbCommand Cmd2 = new OleDbCommand(SQL, Conn))
{
// Cmd2.CommandText = SQL; redundant, the 'new' set the .CommandText
Cmd2.Parameters.AddWithValue("?", Convert.ToInt32(ID));
Cmd2.Parameters.AddWithValue("?", U_Bar);
Cmd2.Parameters.AddWithValue("?", Convert.ToDouble(U_Qty));
Cmd2.Parameters.AddWithValue("?", U_Des);
Cmd2.ExecuteNonQuery();
}
}
Conn.Close();
}
// GC.Collect(); // disabled for test purposes
return true;
I created a database in sql server express edition, in that i create a table called employee. Now i am able to inserting rows(records) dynamically into table successfully, and i can also read those records successfully. But the problem is the values which are inserted dynamically are stored temporarily. When i close and reopen the application the previous inserted records are not available. can u please suggest me what can i do to save records permanently into the database.
thanking you.....
This is my code used to inserting the records into sql server database. Please help me out of this problem...
namespace VACS_practice
{
public partial class Form1 : Form
{
string m_sVehicleNo, m_sName, m_sFlatNo, m_sImagpath;
System.Data.SqlClient.SqlConnection Con;
System.Data.SqlClient.SqlCommand Cmd;
string ConString = #"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\VACSDB.mdf;Integrated Security=True;User Instance=True";
public Form1()
{
InitializeComponent();
}
private void btnAddClick(object sender, EventArgs e)
{
Con = new SqlConnection(ConString);
m_sVehicleNo = m_VehicleNo.Text;
m_sName = m_Name.Text;
m_sFlatNo = m_Phno.Text;
//m_sImagpath = m_ImgPath.Text;
Cmd = new SqlCommand("INSERT INTO ResidentDB ([R_VehNo],[R_Name],[R_PhNo]) VALUES ('" + m_sVehicleNo + "','" + m_sName + "','" + m_sFlatNo + "')", Con);
Con.Open();
Cmd.ExecuteNonQuery();
Con.Close();
MessageBox.Show("Inserted successfully");
// this.Close();
}
Almost certainly you are not committing your changes. If you are running transactions then you must commit.
Alternatively, you are making changes in your in-memory versions, that are not connected to the database at all.
Hey guys, I'm just doin' practise Program to read,write data to Access DB in C# and i'm having a problem in writing data to Access DB though i'm able to read data i.e fetchin' is working fine but when i insert data my "ExecuteNonQuery" is working fine i mean without ne error but when open the Access DB the data is not there.... here is the code what m tryin' to do...
//// Function For ExecuteNonQuery
public static bool ExecuteNonQuery(string Query)
{
OleDbCommand oledbCommand = new OleDbCommand(Query, connection);
if (connection.State == ConnectionState.Open)
connection.Close();
try
{
connection.Open();
if (oledbCommand.ExecuteNonQuery() > 0)
return true;
else
return false;
}
catch (Exception)
{
return false;
}
finally
{
connection.Close();
}
}
This below code is for Adding Data which gets fired on "Add" Button Press
private void btnAdd_Click(object sender, EventArgs e)
{
simOperator.aim_network_name = txtAimNetNm.Text;
simOperator.network_id = txtOxiNetID.Text;
simOperator.network_name = txtNetName.Text;
simOperator.pack_id = txtPackID.Text;
simOperator.pack_name = txtPackName.Text;
SimOperator.Add(simOperator);
fillText();
}
public void fillText()
{
txtResult.Text = "";
SimOperator[] simOperatorList = SimOperator.GetAllOperators();
foreach (SimOperator sm in simOperatorList)
{
txtResult.Text += Program.operator_id + " " + sm.aim_network_name + " " + sm.network_name + " " + sm.network_id + " " + sm.pack_id + " " + sm.pack_name + "\r\n";
}
}
Here's the "Add" Function
string Query = string.Format("insert into {0}({2}) values({1});", calledObject.Name, PropertyValue,PropertyName);
ExecuteNonQuery(Query);
Actuall SQL query is:
insert into SimOperator(aim_network_name,network_id,network_name,pack_id,pack_name) values('FiveNet','2563','FiveNet-Kurla','1236','5236');
Yeah and My Connection String
static OleDbConnection connection = new OleDbConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
App.config file contains string as
add key="ConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\AutoMobileRecharge.mdb;User Id=; Password=;Persist Security Info=True"
The thing m not getting is I have put a Text-Area just beside all the inside fields textboxes so that i can see what is getting inserted, so when insert data that text-area shows the data perfectly but when i open the access db, then theres no data in that, when i close my application and again i ran it then that text-area is empty...this sounds wierd to me.. Any one outthere have faced this kind a problem please help me out here..
Are you doing it during debug ? in that case look if there's an mdf file inside debug folder and if it contains the data you've just inserted. Vs copy some file to debug folder when you run the app in that mode. If i remember correctly there's an option to tell vs not to copy files when debugging