Click here to Skip to main content
15,299,443 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi this is my code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace naft
    public partial class M : Form

        SqlCommand cmd;
        SqlConnection con;
        SqlDataAdapter da;

        public M()
        private void BtnSave_Click(object sender, EventArgs e)
            con = new SqlConnection(@"Data Source=SHADOW\AL;Initial Catalog=Filelib;Integrated Security=True");
            cmd = new SqlCommand("INSERT INTO M (M_Name, M_Code) VALUES (@M_Name, @M_Code)", con);
            cmd.Parameters.Add("@M_Name", TxtM_Name.Text);
            cmd.Parameters.Add("@M_Code", TxtM_Code.Text);

when i want to insert name and code it show me this error : invalid object name Naft ( after i change it to "m" it says invalid object name "m" )

"naft" is my sql database and the table that i want to insert data is "M" how to fix it ?

What I have tried:

i searched a lot but i can't fix it.
Updated 13-Feb-21 0:19am
[no name] 15-Mar-16 8:47am
Make sure that table "M" is available in "Filelib" database.
brandon1999 15-Mar-16 9:16am
thanks fixed it.
Mohammad Reza Valadkhani 15-Mar-16 8:59am
are you sure that table and filed exists?!
brandon1999 15-Mar-16 9:05am
yes i'm sure
Mohammad Reza Valadkhani 15-Mar-16 9:11am
double check your database, if you have enough access try to check your query interaction in your SQL server to find out the error, technically this error happen when your table or filed is missing.
brandon1999 15-Mar-16 9:15am
thanks i fixed it.
Jochen Arndt 15-Mar-16 9:16am

"naft" is my sql database

does not correspond to your connection string ("Filelib" database).

1 solution

"naft" is my sql database
Then your connection string should look like this:
@"Data Source=SHADOW\AL;Initial Catalog=naft;Integrated Security=True"

And then your query should work like you've shown it ("INSERT INTO M ...") provided that there are no mistakes with the column names and types.

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