Click here to Skip to main content
15,892,804 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys
i am inserting a string and a int into a database. There is no error but the code below didn't work. i am thinking it is the connectionstring which work on my WPF application. Can you please advise me on what went wrong. TIA! :D

C#
string a = textBox1.Text;
            string b = comboBox1.Text;
            string c = a + b;
            int l = 0;
            if (textBox1.Text == "")
            {
                MessageBox.Show("Please Enter a Video's Name!", "Information",
                   MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (comboBox1.Text == "")
            {
                MessageBox.Show("Please select Video's extension", "Information",
                   MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {

                SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Video.MDF;Integrated Security=True;User Instance =True");
                SqlCommand comm = new SqlCommand("INSERT INTO Video VALUES('" + a + b + "'," + l + ")", con);
                SqlDataAdapter da = new SqlDataAdapter(comm);
                con.Open();
                comm.ExecuteNonQuery();
}
Posted
Updated 27-Jan-13 18:43pm
v3

Hi,

Nothing looks suspicious in your code except for the ConnectionString...
What does AttchDbFileName do??

Anyways you might want to try a normal ConnectionString as below:

C#
connectionString="Data Source=MY-PC;Initial Catalog=Sample; Integrated Security=true


Let us know if it works..

Cheers
 
Share this answer
 
Comments
christine tan 28-Jan-13 1:31am    
thanks for helping
the connectionstring i use is for the DB in the visual studio not sql management studio
Anurag Sinha V 28-Jan-13 3:59am    
I see...to be very frank,,your code doesn't look error-prone...
Try debugging..that is what I can say...
Also, how do we access DB's in VIsual Studio itself..haven't heard of this...

Regards
Debug your code with a break point. I hope that will help you in correcting connection string when you actually got the error. Other wise its is fine.
also try this
SqlCommand comm = new SqlCommand("INSERT INTO Video VALUES('" + (a + b) + "'," + l + ")", con);

instead of
SqlCommand comm = new SqlCommand("INSERT INTO Video VALUES('" + a + b + "'," + l + ")", con);
 
Share this answer
 
v2
Comments
christine tan 28-Jan-13 1:31am    
thanks for helping
there isnt any error when i run, all of it seem alright
I have solve it, the problem lies on the database. i have change the database to sql server 2008 instead of the one in visual studio

thanks everyone for your help :D
 
Share this answer
 
Comments
Abhishek Pant 28-Jan-13 4:15am    
sometimes researches[^] are more necessary for question.
guruprakashc 28-Jan-13 5:51am    
if you got the answer mark as answered
Sushil Mate 28-Jan-13 5:55am    
"i have change the database to sql server 2008 instead of the one in visual studio"

What does it mean?
SqlCommand comm = new SqlCommand("INSERT INTO Video VALUES('" + a + b + "',1)", con);
 
Share this answer
 

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