4

I'm having an issue at the moment which I am trying to fix. I just tried to access a database and insert some values with the help of C#

My code:

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();

builder.DataSource = "testtetstet.database.windows.net";
builder.UserID = "PW";
builder.Password = "000000000";
builder.InitialCatalog = "test";

using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
{
    String query = "INSERT INTO dbo.amir (theDate,Hostname,IP) VALUES (@theDate,@hostName, @IP)";

    using (SqlCommand command = new SqlCommand(query, connection))
    {
        command.Parameters.Add("@theDate");
        command.Parameters.Add("@hostName");
        command.Parameters.Add("@IP");

        connection.Open();
        var result = command.ExecuteNonQuery();

        // Check Error
        if (result < 0)
            Console.WriteLine("Error inserting data into Database!");
    }
}

What I'm trying to do is, insert 3 strings in to the sql database but it seems that it does not work!!

"@hostname"is a string which contains the computer name "@IP" is a string which contains the IP adress "@theDate" is a DateTime string which display date and time.

What am I doing wrong?

8
  • 6
    Isn't it obvious? You're not supplying any values to the parameters you're adding. Try command.Parameters.Add("@theDate").Value = DateTime.UtcNow, etc. Commented Oct 4, 2018 at 6:20
  • what error/exception you are getting exactly? Please put try catch blocks to handle the exception Commented Oct 4, 2018 at 6:21
  • As per John's comment, you probably should read Msdn SqlCommand.Parameters Property Commented Oct 4, 2018 at 6:23
  • @John Correct. You should pass the values. Values may be from textbox or any object. Please check Refer this code: command.Parameters.Add("@theDate",dateValue); command.Parameters.Add("@hostName",hostValue); command.Parameters.Add("@IP",IpValue); Commented Oct 4, 2018 at 6:23
  • I also suggest setting the sql db type for each parameter. Commented Oct 4, 2018 at 6:25

2 Answers 2

7

You are not setting the value of parameter.You should either Use Parametrs.Add like this:

command.Parameters.Add("@IP", SqlDbType.VarChar).Value = "0.0.0.0";
//equals to:
//command.Parameters.Add("@IP", SqlDbType.VarChar); 
//command.Parameters["@IP"].Value = "0.0.0.0";

or (not recommended):

command.Parameters.AddWithValue("@IP", "0.0.0.0");
Sign up to request clarification or add additional context in comments.

4 Comments

I was just showing possible ways, not recommending one.
Well, you should be recommending one. After all, we are here to help...
Thank you Ashkan!
6

You don't pass any values to the parameters. Try something like this:

command.Parameters.AddWithValue("@theDate", date);
command.Parameters.AddWithValue("@hostName", host);
command.Parameters.AddWithValue("@IP", ip);

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.