An object or column name is missing or empty (0x80131904) - c#

I'm getting this error when I try to use the button to restore the database:
"An object or column name is missing or empty".
The thing is that I'm using offline database file (.mdf) and when I click ok on this error and press the restore button again, it works like a charm.
There's the code:
private void RestoreButton_Click(object sender, EventArgs e)
{
string database = con.Database.ToString();
if (con.State!=ConnectionState.Open)
{
con.Open();
}
try
{
string sqlStmt2 = string.Format("ALTER DATABASE [" + database + "] SET SINGLE_USER WITH ROLLBACK IMMEDIATE");
SqlCommand bu2 = new SqlCommand(sqlStmt2, con);
bu2.ExecuteNonQuery();
string sqlStmt3 = "USE MASTER RESTORE DATABASE [" + database + "] FROM DISK='" + textBox2.Text + "'WITH REPLACE;";
SqlCommand bu3 = new SqlCommand(sqlStmt3, con);
bu3.ExecuteNonQuery();
string sqlStmt4 = string.Format("ALTER DATABASE [" + database + "] SET MULTI_USER");
SqlCommand bu4 = new SqlCommand(sqlStmt4, con);
bu4.ExecuteNonQuery();
MessageBox.Show("Database succesfully restored!");
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
There's the full error.

Related

MySqlDataReader executes command, but reader.Read() outputs error message

I do not really know how to ask this well, but will try to describe.
I have created a mysql command, that should insert data into a table and it does exactly what I want, but if statement reader.Read() jumps into an error saying it failed to create the user, even though the user was created.
Why is this happening?
private void iconButton1_Click(object sender, EventArgs e)
{
//create user in mysql
MySqlConnection conn = new MySqlConnection(#"SERVER=localhost;DATABASE=sklad;UID=root;PASSWORD=;");
conn.Open();
MySqlCommand cmd = new MySqlCommand("INSERT INTO users (username, password, role) VALUES ('" + username.Text + "', '" + password.Text + "', '" + role.Text + "') ", conn);
MySqlDataReader reader = cmd.ExecuteReader();
if(reader.Read())
{
MessageBox.Show("Uživatel vytvořen!", "E-Skladovka.cz - Uživatel vytvořen", MessageBoxButtons.OK);
}
else
{
MessageBox.Show("Failed to create user");
}
reader.Close();
cmd.Dispose();
conn.Close();
}
I tried to search, but as long as I do not know how to ask properly, I can not search properly.

creating connection string for attached MDF file

what you see bellow is a part of my WPF project I used it to backup/restore my SQL server database and it works great but; when I want to make a setup file by advanced installer and I move MDF and LDF files created by SQL script to app directory to use in SQL express it doesn't work. Now I want to know what can I do with connection string so that backup/restore prosses can work correctly
private string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFileName=|DataDirectory|\\DBNBO.mdf;Database=DBNBO; Trusted_Connection=Yes;";
private void BtnBackup_OnClick(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection(connectionString);
string database = connection.Database;
string query = "Backup Database [" + database + "] To Disk = '" + txtBackup.Text + "\\DB_backup.bak'";
SqlCommand command = new SqlCommand(query, connection);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
private void btnRestore_Click(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection(connectionString);
string database = connection.Database;
if (connection.State != ConnectionState.Open) connection.Open();
string query1 = string.Format("Alter Database [" + database + "] Set Single_User With Rollback Immediate");
SqlCommand command1 = new SqlCommand(query1, connection);
command1.ExecuteNonQuery();
string query2 = string.Format("Use Master Restore Database [" + database + "] From Disk = '" + txtRestore.Text + "' With Replace");
SqlCommand command2 = new SqlCommand(query2, connection);
command2.ExecuteNonQuery();
string query3 = string.Format("Alter Database [" + database + "] Set Multi_User");
SqlCommand command3 = new SqlCommand(query3, connection);
command3.ExecuteNonQuery();
connection.Close();
}

Invalid object name 'Main' error when inserting into Database - C# (WebForms), MySql

I know plenty of people have these issues, and I've actually tried to implement some of the suggestions to my code, however I'm getting errors that just don't make sense to me. This is my first time implementing database calls to my code. Can someone please tell me what I'm doing wrong? The following error pops up: ERROR: Invalid object name 'Main'. This is actually triggered by my exception so at least something is working. Otherwise, I don't know what the issue is. On the DB end, I have (username VARCHAR, email VARCHAR and number NCHAR) Please see the code below
static string path = Path.GetFullPath(Environment.CurrentDirectory);
static string databaseName = "u_DB.mdf";
string connectionString = #"Data Source=(localdb)\MSSQLLocalDB;AttachDbFilename=" + path + #"\" + databaseName + "; Integrated Security=True;";
private void button1_Click(object sender, EventArgs e)
{
// string query = "INSERT INTO UserInfo '" + textBox1.Text + "' and password = '" + textBox2.Text + "'";
string query = "insert into Main ([username], [email], [number]) values(#username,#email,#number)";
using (SqlConnection con = new SqlConnection(connectionString))
{
try
{
con.Open();
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.Add("#username", SqlDbType.VarChar).Value = textBox3.Text;
cmd.Parameters.Add("#email", SqlDbType.VarChar).Value = textBox2.Text;
cmd.Parameters.AddWithValue("#number", SqlDbType.VarChar).Value = textBox1.Text;
int rowsAdded = cmd.ExecuteNonQuery();
if (rowsAdded > 0)
MessageBox.Show("Added to Database");
else
MessageBox.Show("Nothing was added");
}
}
catch (Exception ex)
{
MessageBox.Show("ERROR: " + ex.Message);
}
con.Close();
}
}
Firstly, as Chetan assumed, do you have a main table?
The syntax of the query you are using is :
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Furthermore,
AddWithValue(string parameterName, object value (<== The actual value to insert!));
in your case
AddWithValue("#number", textBox1.Text);
is enough.

Adding one value and insert it into two tables in asp.net c# using SQL query but in one table it is stored as primary key and autoincrement

enter image description hereI have two tables where in table tbl_mngr_shift_handover there mgr_handover_id is primary key and i want to insert it into another table tbl_mngr_shift_handover_details at insert time. So following is asp.net c# code
string remarkString = remarkTextArea.Content;
string query = string.Empty;
query="INSERT INTO tbl_mngr_shift_handover(msp_id,cat_id,task_id,priority_id,shift_id,status_id,activity)VALUES('"
+MSPDropDownList.SelectedValue+"','"
+CategoryDropDownList.SelectedValue+"','"
+TaskDropDownList.SelectedValue+"','"
+PriorityDropDownList.SelectedValue+"','"
+ShiftDropDownList.SelectedValue+"','"
+StatusDropDownList.SelectedValue+"','"
+ActivityTextBox.Text+"')";
string query1 ="INSERT INTO tbl_mngr_shift_handover_details(status_id,remark,inserted_by,inserted_date)"
+"VALUES('" + StatusDropDownList.SelectedValue + "','"
+ remarkTextArea.Content + "','"
+ Session["user_id"] + "','"
+ DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "')";
if(query != null)
{
string query2 = "update tbl_mngr_shift_handover_details set tbl_mngr_shift_handover_details.mgr_handover_id=tbl_mngr_shift_handover.mgr_handover_id from tbl_mngr_shift_handover where tbl_mngr_shift_handover_details.mgr_handover_id=tbl_mngr_shift_handover.mgr_handover_id ";
con.ConnectionString = constr;
con.Open();
if (con != null)
{
cmd1 = new SqlCommand(query2, con);
cmd1.ExecuteNonQuery();
con.Close();
}
}
try
{
con.ConnectionString = constr;
con.Open();
if (con != null)
{
cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
cmd2 = new SqlCommand(query1, con);
cmd2.ExecuteNonQuery();
PopUpLabel.Text = "Inserted Successfully";
ModalPopupExtender2.Show();
}
}
catch (Exception er)
{
// Label3.Text += "error in handover : " + er.Message;
PopUpLabel.Text = "Error While Inserting Handover.Please Concern Your Manager: Error Details: " + er.Message;
pnlPopup.Visible = true;
ModalPopupExtender2.Show();
}
finally
{
con.Close();
}
Here is database fields Images
enter image description here
Here In tbl_mngr_shift_handover_details field mgr_handover_id show null vallue so what can i do..

C# store data error in Microsoft Access database

private void okbtn_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Desktop\GameMuseumManagementSystem.accdb";
try
{
conn.Open();
String Name = txtName.Text.ToString();
String Email = txtEmail.Text.ToString();
String Password = txtPassword.Text.ToString();
String my_query = "INSERT INTO Member(Member_Name,Member_Password,Member_Email)VALUES('" + Name + "','" + Email + "','" + Password + "')";
OleDbCommand cmd = new OleDbCommand(my_query, conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Data saved successfuly...!");
}
catch (Exception ex)
{
MessageBox.Show("Failed due to" + ex.Message);
}
finally
{
conn.Close();
}
}
I am coding for the member registeration for a guest to use it. I have 3 pieces of data, member_name, member_ID, and password. I coded this and I get an error. My Visual Studio is connected to my MS Access database via the tools, after I write this code, the data can't be stored in Access, what should I do now? Any suggestion?

Categories