Ok I have some VB code that does my Log in and I want it to now work in C#. I thought this would be pretty straight forward, however I was wrong. The line: string connString = ConfigurationManager.ConnectionStrings(“MyConnection”).ConnectionString; is throwing an error at ConnectionStrings and I can’t figure out why. Also in the while statement the objDR says that it it is a variable but is being used as a method. Any help would be great. The following is the entire code:
Using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void
btnSubmit_Click(object sender, System.EventArgs e)
{
if (((string.IsNullOrEmpty(txtUserName.Text))))
{
lblErrorMessage.Text = "Username must be entered.";
txtUserName.Focus();
return;
}
string connString = ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString;
System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(connString);
string sql = "Select * From TCustomers";
System.Data.SqlClient.SqlDataReader objDR = default(System.Data.SqlClient.SqlDataReader);
System.Data.SqlClient.SqlCommand objCmd = new System.Data.SqlClient.SqlCommand(sql, myConnection);
myConnection.Open();
objDR = objCmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
bool blnLogin = false;
string strPassword = null;
string strUserName = null;
strPassword = txtPassword.Text;
strPassword = strPassword.Trim();
strUserName = txtUserName.Text;
strUserName = strUserName.Trim();
while (objDR.Read())
{
if (((objDR("strUserName").ToString().Trim() == strUserName)) & ((objDR("strPassword").ToString().Trim() == strPassword)))
{
blnLogin = true;
Session["CustomerID"] = objDR("intCustomerID");
Session["UserName"] = objDR("strUserName");
Session["FirstName"] = objDR("strFirstName");
Session["LastName"] = objDR("strLastName");
Session["Email"] = objDR("strEmailAddress");
Session["UserType"] = objDR("intUserTypeID");
break; // TODO: might not be correct. Was : Exit While
}
}
}
}
In VB, there is no syntax difference between a method call or an array access, they both use
(argument). In C# however, arrays use[]. This is not properly converted with automatic/rote conversion because it’s impossible to tell the difference, so you have to fix it yourself:Same with accessing the properties of the DataRow: