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();
        }
1
  • webtable has two columns Id and url_name both of type varchar....and I also remember to refresh every time Commented Apr 10, 2015 at 6:31

3 Answers 3

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();
Sign up to request clarification or add additional context in comments.

8 Comments

copy pasted this one...still no data appears in table
just change con to conn in sqlcommand line..its work fine for me.
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?
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?
Its gives any error while inserting data into database?
|
0

Try to Assign Data Types for the Parameters.

Refer this,

[SqlCommand Parameters Add vs. AddWithValue

4 Comments

did you encounter any error while inserting. post the error if you get anything
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);
It displays the inserted data here in the select but not in the table and if I run just the select again...nothing there
Try Using Transaction to Commit your Data SqlTransaction transaction = null; transaction = con.BeginTransaction(); cmd.Transaction = transaction; //execute your query transaction.Commit();
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();

}

1 Comment

sorry, its conn.Open(); and conn.Close(); I have edited the code try it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.