Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hyy all,

I am having problem with insert query in my C# application. i m working with sql 2000 database and visual studio 2005.

below is the code that i have written,

C++
SqlConnection sqlcon = new SqlConnection("DATA SOURCE=SERVER;Database=DISPATCH.MDF;Trusted_Connection=True;");
        SqlCommand cmd;

        public frmClients()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                sqlcon.Open();
                SqlCommand cmd = new SqlCommand("Insert INTO tblContacts (CLIENTS) VALUES('" + textBox1.Text + "')");
                cmd.ExecuteNonQuery();
                
            }
            catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }   
        }
    }


everything seems to be fine, but as soon as the save button is executed, an error pops up saying "cannot open database requested in login 'db.mdf'. Login fails. Login failed for server\administrator.

Although I am using a trusted connection, even then this problem persists.
Any help is appreciated thanks.

P.s. I m new to C#. I am using Windows Authentication for SQL DATABASE.
Posted
Updated 28-Aug-14 23:15pm
v2
Comments
Gihan Liyanage 29-Aug-14 5:21am    
did you loggin as Admin ?
Herman<T>.Instance 29-Aug-14 5:30am    
do not use the .MDF that is the filename and not the database name!
fahadee2 29-Aug-14 5:48am    
ok i removed the .mdf and added sqlcon.open(); before executenonequery then upon button click the error generates that connection state is open.
Herman<T>.Instance 29-Aug-14 6:16am    
after cmd.ExecuteNonQuery();
you must sate: cmd.Close();
Otherwise the connecion to the database remains open
Gihan Liyanage 29-Aug-14 5:37am    
Ohh Yes, good track, DISPATCH.MDF is mdf file name..

Hello ,

You have not passed the SqlConnection as a second parameter into the command . Therefore the error comes .

Try this way
SqlCommand cmd = new SqlCommand("Insert INTO tblContacts (CLIENTS) VALUES('" + textBox1.Text + "')",sqlcon);


thanks
 
Share this answer
 
OK I got it.

In my sqlCommand I was not executing the command on the connection. I added , sqlcon at the end of my command
the new Command became like

sqlcommand cmd = new sqlcommand ("insert into table (Column) values('" + textbox1.text + "')", sqlcon);

This solved my problem...

Thanks Anyway for the help...!
 
Share this answer
 
Comments
Gihan Liyanage 29-Aug-14 6:23am    
Nice to here that you have solved your self
Animesh Datta 29-Aug-14 6:24am    
if your problem is solved then accept the answer
select appropriate connection string from here

https://www.connectionstrings.com/sql-server-2000/[^]
 
Share this answer
 
Comments
fahadee2 29-Aug-14 5:32am    
I've tried the connection strings. None seems to work. I am using Windows Authentication for my SQL Database.
try using this :
XML
Server=[SERVER];Database=[DATABASE];Trusted_Connection=True;

Fixed your curly brace typo.
 
Share this answer
 
v2
Comments
fahadee2 29-Aug-14 5:40am    
Could this error be caused by the fact that I am using SQL Server 2000 and the Database is attached into Enterprise Manager, and not into Visual Studio 2005.

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