P
Patrick Olurotimi Ige
I converted the code below from VB.NET to C# cos i have to add it to a
C# application!!
But i'm getting the error:-
System.Data.SqlClient.SqlCommand.Parameters' denotes a 'property' where
a 'method' was expected
At the lines:-
myCommand.Parameters("@Username").Value = strLogonUsr;
myCommand.Parameters("@DateCreated").Value = Now();
I guess i am missing something can a C# Guru help
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
string strLogonUsr;
strLogonUsr = Request.ServerVariables["LOGON_USER"];
myconnection = new
SqlConnection("server=(local);database=Wintergreen;integrated
security=true;");
string insertCmd = "insert into survey values (@username,
@DateCreated)";
myCommand = new SqlCommand(insertCmd, myconnection);
//Created new parameters for the SqlCommand object and
//initialize them to the field values.
//Create the parameter for the logon user
myCommand.Parameters.Add(new
SqlParameter("@Username",SqlDbType.VarChar, 50));
//Assigned the value
myCommand.Parameters("@Username").Value = strLogonUsr;
//Created the parameter for the time
myCommand.Parameters.Add(new
SqlParameter("@DateCreated",SqlDbType.DateTime, 8));
//Assign the value as Now()
myCommand.Parameters("@DateCreated").Value = Now();
myCommand.Connection.Open();
try
{
// Execute the command
myCommand.ExecuteNonQuery();
// Report success
Label1.Text = "<b>Thanks for filling Survey!</b>";
}
catch (SqlException exc)
{
// Report error if exist
Label1.Text = exc.Message;
}
finally
//Close the connection no matter what
{
myCommand.Connection.Close();
}
}
C# application!!
But i'm getting the error:-
System.Data.SqlClient.SqlCommand.Parameters' denotes a 'property' where
a 'method' was expected
At the lines:-
myCommand.Parameters("@Username").Value = strLogonUsr;
myCommand.Parameters("@DateCreated").Value = Now();
I guess i am missing something can a C# Guru help
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
string strLogonUsr;
strLogonUsr = Request.ServerVariables["LOGON_USER"];
myconnection = new
SqlConnection("server=(local);database=Wintergreen;integrated
security=true;");
string insertCmd = "insert into survey values (@username,
@DateCreated)";
myCommand = new SqlCommand(insertCmd, myconnection);
//Created new parameters for the SqlCommand object and
//initialize them to the field values.
//Create the parameter for the logon user
myCommand.Parameters.Add(new
SqlParameter("@Username",SqlDbType.VarChar, 50));
//Assigned the value
myCommand.Parameters("@Username").Value = strLogonUsr;
//Created the parameter for the time
myCommand.Parameters.Add(new
SqlParameter("@DateCreated",SqlDbType.DateTime, 8));
//Assign the value as Now()
myCommand.Parameters("@DateCreated").Value = Now();
myCommand.Connection.Open();
try
{
// Execute the command
myCommand.ExecuteNonQuery();
// Report success
Label1.Text = "<b>Thanks for filling Survey!</b>";
}
catch (SqlException exc)
{
// Report error if exist
Label1.Text = exc.Message;
}
finally
//Close the connection no matter what
{
myCommand.Connection.Close();
}
}