Wednesday, March 28, 2012

Help with sql connection in C#

I have always programmed in VB.net but was given this site that is written in C#. Basically for some reason they can not get the

IDataReader reader

To work on the IIS server they are loading it on to. They are now asking me to fix the website so it will work. I have no clue how to do this in C#.

In VB.net I would do this:

Dim ConnectionString As String = "server=;uid=;pwd=;database="
Dim CommandText As String
Dim myConnection As New SqlConnection(ConnectionString)
Dim Adapter As SQLDataAdapter = New SQLDataAdapter
Dim MyCommandBuilder As SQLCommandBuilder
Dim MatcherDS As DataSet = New DataSet
Dim Row As DataRow
Dim Count

SelectStatement = " select M_ID, M_FirstName, M_LastName, M_Email, M_Login, M_Password, M_Level from Member where M_Login = '" & txtuser.text & "' and M_Password COLLATE Latin1_General_CS_AS = '" & txtpassword.text & "'"
Adapter.SelectCommand = New SQLCommand(SelectStatement, MyConnection)
MyCommandBuilder = New SQLCommandBuilder(Adapter)
Adapter.Fill(MatcherDS, "temp")

count = MatcherDS.Tables("temp").Rows.Count

if count > 0 then
Row = MatcherDS.Tables("temp").Rows(0)
Session("M_FLevel") = Row.item("M_Level")
Session("M_ID") = Row.item("M_ID")
if Session("M_FLevel") = 0 then
Session("M_FLevel") = 5
end if

Response.Redirect("home.aspx")

else

response.write("Error Login")

end if

Now my question is how do I program this in C# so I can open the sql server and access the tables the same way??

Any help is appreciated...

The same the way you do it in vb.net except you use c#. In .NET all thelanguages use the same objects, but may differ in syntax. Look for avb.net to c# convertor on the net.

Rahul|||

Below is the C# equivelent.

string ConnectionString ="server=;uid=;pwd=;database=";
string CommandText;
SqlConnection myConnection =new SqlConnection(ConnectionString);
SqlDataAdapter Adapter =new SqlDataAdapter;
SqlCommandBuilder MyCommandBuilder;
DataSet MatcherDS =new DataSet;
DataRow Row;
int Count;

SelectStatement =" select M_ID, M_FirstName, M_LastName, M_Email, M_Login, M_Password, M_Level from Member where M_Login = '" + txtuser.Text +"' and M_Password COLLATE Latin1_General_CS_AS = '" + txtpassword.Text +"'";
Adapter.SelectCommand =new SqlCommand(SelectStatement, myConnection);
MyCommandBuilder =new SqlCommandBuilder(Adapter);
Adapter.Fill(MatcherDS,"temp");

count = MatcherDS.Tables["temp"].Rows.Count;

if (count > 0)
{
Row = MatcherDS.Tables["temp"].Rows[0];
Session["M_FLevel"] = Row.Item["M_Level"];
Session["M_ID"] = Row.Item["M_ID"];

if ((int)Session["M_FLevel"] == 0)
{
Session["M_FLevel"] = 5;
}

Response.Redirect("home.aspx");
}
else
{
Response.Write("Error Login");
}

HTH

|||Thank you, that worked...now I have something to working to work off of on the rest of this code. Thank you for your help...sql

No comments:

Post a Comment