Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL WindowsForms , +
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! Big Grin | :-D
 
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 27-Jan-13 19:37pm
Edited 27-Jan-13 19:43pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 
connectionString="Data Source=MY-PC;Initial Catalog=Sample; Integrated Security=true
 
Let us know if it works..
 
Cheers
  Permalink  
Comments
christine tan at 28-Jan-13 1:31am
   
thanks for helping
the connectionstring i use is for the DB in the visual studio not sql management studio
Anurag3487 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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);
  Permalink  
v2
Comments
christine tan at 28-Jan-13 1:31am
   
thanks for helping
there isnt any error when i run, all of it seem alright
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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 Big Grin | :-D
  Permalink  
Comments
Abhishek Pant at 28-Jan-13 4:15am
   
sometimes researches[^] are more necessary for question.
suryaguruprakash at 28-Jan-13 5:51am
   
if you got the answer mark as answered
Sushil Mate at 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?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

 SqlCommand comm = new SqlCommand("INSERT INTO Video VALUES('" + a + b + "',1)", con);
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 375
1 Sergey Alexandrovich Kryukov 329
2 CPallini 260
3 DamithSL 214
4 Maciej Los 197
0 OriginalGriff 5,455
1 DamithSL 4,457
2 Maciej Los 3,885
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,115


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 28 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100