Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a problem when I'm writing this code in C#. It was giving me the error that SQLDatareader cannot be used as a constructor.
Please provide me urgent answer with full converted coding.

-----------------------THE VB.NET--CODING---------------------------------
VB
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cmd As New SqlCommand("select * from Exam_master", con)
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If

        dr = cmd.ExecuteReader()
        While dr.Read()
            If dr("Stud_id") = TextBox1.Text Then
                Label16.Text = ""
                Label8.Text = dr("Stud_id")
                Label10.Text = dr("Course_id")
                Label9.Text = dr("Name")
                Label14.Text = dr("Class")
                Label11.Text = dr("Maximum")
                Label12.Text = dr("Minimum")
                Label13.Text = dr("Total")
                Exit While
            Else
                Label16.Text = "Invalid Student......"
                Label8.Text = ("-------------")
                Label10.Text = ("-------------")
                Label9.Text = ("-------------")
                Label14.Text = ("-------------")
                Label11.Text = ("-------------")
                Label12.Text = ("-------------")
                Label13.Text = ("-------------")
            End If
        End While
    End Sub
Posted
Updated 25-Mar-12 8:04am
v2
Comments
[no name] 25-Mar-12 14:05pm    
Edited content, removed shouting and urgent.
Shahin Khorshidnia 25-Mar-12 14:06pm    
label16.Text = label8.Text = label10.Text = label9.Text = label14.Text = label11.Text = label12.Text = label13.Text = ("-------------")

It's better. isn't it?

 
Share this answer
 
You have to declare variables before using them in C#.

C#
DataReader dr = cmd.ExecuteReader();


should work for you.
 
Share this answer
 
v2
Comments
sankyn1 25-Mar-12 14:41pm    
i will chek out and answer you becoz it was still not working
sankyn1 25-Mar-12 14:47pm    
its not working it was givin error like===>Error 2 'dr' is a 'variable' but is used like a 'method' E:\ITPLUS\result.aspx.cs 38 31 E:\ITPLUS\
[no name] 25-Mar-12 14:50pm    
Change
if (dr("Stud_id") == TextBox1.Text) {
to

if (dr["Stud_id"] == TextBox1.Text) {

note the square brackets
sankyn1 25-Mar-12 15:17pm    
ok im trying
sankyn1 25-Mar-12 15:21pm    
hey thanks its really workin thanks a lot
Try this:

C#
protected void Button1_Click(object sender, System.EventArgs e)
{
    SqlCommand cmd = new SqlCommand("select * from Exam_master", con);
    if (con.State == ConnectionState.Closed) {
        con.Open();
    }

    dr = cmd.ExecuteReader();
    while (dr.Read()) {
        if (dr("Stud_id") == TextBox1.Text) {
            Label16.Text = "";
            Label8.Text = dr("Stud_id");
            Label10.Text = dr("Course_id");
            Label9.Text = dr("Name");
            Label14.Text = dr("Class");
            Label11.Text = dr("Maximum");
            Label12.Text = dr("Minimum");
            Label13.Text = dr("Total");
            break; // TODO: might not be correct. Was : Exit While
        } else {
            Label16.Text = "Invalid Student......";
            Label8.Text = ("-------------");
            Label10.Text = ("-------------");
            Label9.Text = ("-------------");
            Label14.Text = ("-------------");
            Label11.Text = ("-------------");
            Label12.Text = ("-------------");
            Label13.Text = ("-------------");
        }
    }
}


hope it helps :)
 
Share this answer
 
Comments
sankyn1 25-Mar-12 14:02pm    
but it was giving error that dr cann not be used as a constructor
mariazingzing 25-Mar-12 14:13pm    
do not accept flawed answers
Uday P.Singh 25-Mar-12 15:05pm    
I just converted the vb code to C#, and the error you are getting is just because it is in your vb code, where did you declare your DataReader?
mariazingzing 25-Mar-12 14:11pm    
Error
Uday P.Singh 25-Mar-12 15:06pm    
and the error is?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900