0

Hi all I have been stuck at this for two days. I am using Visual Studio 2013 and made a windows form project. Then using the following code I am trying to insert values but the "show Table data" option produces a table with null values. What have I done wrong?

        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.
 ConnectionStrings["WindowsFormsApplicationtodatabase.Properties.Settings.DatabasewebConnectionString"].ConnectionString);

        conn.Open();

        using (conn)
        {
            SqlCommand cmd = new SqlCommand("INSERT INTO webtable (Id, url_name) VALUES (@id, @url)");
            cmd.CommandType = CommandType.Text;
            cmd.Connection = conn;
            cmd.Parameters.AddWithValue("@url", "nagia");
            cmd.Parameters.AddWithValue("@id", "9");

            cmd.ExecuteNonQuery();
        }
SelvaS
  • 2,105
  • 1
  • 22
  • 31
aneena
  • 187
  • 1
  • 2
  • 13
  • webtable has two columns Id and url_name both of type varchar....and I also remember to refresh every time – aneena Apr 10 '15 at 06:31

3 Answers3

1

You can try this

 conn.Open();

 string strQuery = "INSERT INTO webtable (Id, url_name) VALUES (@id, @url)";
 SqlCommand cmd = new SqlCommand(strQuery, conn);
 cmd.Parameters.AddWithValue("@url", "nagia");
 cmd.Parameters.AddWithValue("@id", "9");
 cmd.Connection = conn;
 cmd.ExecuteNonQuery();
 conn.Close();
Pradnya Bolli
  • 1,915
  • 1
  • 19
  • 37
  • copy pasted this one...still no data appears in table – aneena Apr 10 '15 at 06:36
  • just change con to conn in sqlcommand line..its work fine for me. – Pradnya Bolli Apr 10 '15 at 06:41
  • I did change that...why isnt it working for me. while adding the database in the windows form project I added the "service based database" ...well i hope that isnt causing trouble? – aneena Apr 10 '15 at 06:45
  • I know my connection is fine because when I added some data manually (clicking on the table gave the option) and ran the select query in my c# file ...it worked fine...but data isnt getting inserted through my code...why? – aneena Apr 10 '15 at 06:48
  • Its gives any error while inserting data into database? – Pradnya Bolli Apr 10 '15 at 06:49
  • So check connection string...check this site http://stackoverflow.com/questions/15792664/inserting-data-to-a-database-visual-studio hope this useful. – Pradnya Bolli Apr 10 '15 at 06:56
  • you can write connection string in this format "Data Source=.\SQLEXPRESS;AttachDbFilename=" + "|DataDirectory|pr.mdf;" + "Integrated Security=True;User Instance=True"; When I replaced |DataDirectory| with the full path to the DB (right click on the DB in the solution explore, click properties and then use the full path). It worked. It seams as though there are a couple of different DB copies...i.e. there is a copy of it in the \bin\Debug\ folder. – Pradnya Bolli Apr 10 '15 at 06:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/74901/discussion-between-pradnya-bolli-and-aneena). – Pradnya Bolli Apr 10 '15 at 06:58
0

Try to Assign Data Types for the Parameters.

Refer this,

[SqlCommand Parameters Add vs. AddWithValue

Community
  • 1
  • 1
  • did you encounter any error while inserting. post the error if you get anything – Sarathi Jayapal Apr 10 '15 at 06:39
  • When I run both insert and select in the same run like this string strQuery = "INSERT INTO webtable (Id, url_name) VALUES (@id, @url)"; SqlCommand cmd = new SqlCommand(strQuery, conn); cmd.Parameters.AddWithValue("@url", "nagia"); cmd.Parameters.AddWithValue("@id", "9"); cmd.Connection = conn; cmd.ExecuteNonQuery(); String qry = " select * from dbo.webtable"; SqlDataAdapter da = new SqlDataAdapter(qry, conn); DataSet ds = new DataSet(); da.Fill(ds); – aneena Apr 10 '15 at 07:00
  • It displays the inserted data here in the select but not in the table and if I run just the select again...nothing there – aneena Apr 10 '15 at 07:00
  • Try Using Transaction to Commit your Data SqlTransaction transaction = null; transaction = con.BeginTransaction(); cmd.Transaction = transaction; //execute your query transaction.Commit(); – Sarathi Jayapal Apr 10 '15 at 07:25
0

You can try this code..

using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["WindowsFormsApplicationtodatabase.Properties.Settings.DatabasewebConnectionString"].ConnectionString));
{

SqlCommand cmd = new SqlCommand("INSERT INTO webtable (Id, url_name) VALUES (@id, @url)",conn);

cmd.Parameters.AddWithValue("@url", "nagia");
cmd.Parameters.AddWithValue("@id", "9");
cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();

}
Kevin Andrid
  • 1,963
  • 1
  • 15
  • 26