Click here to Skip to main content
15,908,274 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Can any one help me for this:
public static void SelectMembers(string username)
    {
        SqlConnection con = Database.GetConnection();
        SqlCommand cmd = new SqlCommand("select * from members where username=@" + username + "", con);
        cmd.ExecuteNonQuery();
    }


I use this function in a class file, but I want to use this function in sqldatareader, how can I call it in sqldatareader.
Ex:
SqlDataReader dr=cmd.ExecuteReader();

but now this function how can i call???? Because now i have no cmd in this file. its written in only class file.
Posted
Updated 13-May-11 23:58pm
v3

That doesn't make a lot of sense.
You have a function SelectMembers that (badly, and prone to SQL Injection attack) gets the count of the number of members whose username matches the parameter, and then throws it away.

Then you want to do something different, but you can't access the internals of the method.

What are you trying to do?

If you want to read the member details, then cut and paste your two code fragments together and create a new method:
public static void SelectMembersAndActuallyReadThemThisTime(string username)
    {
        SqlConnection con = Database.GetConnection();
        SqlCommand cmd = new SqlCommand("select * from members where username=@" + username + "", con);
        SqlDataReader dr=cmd.ExecuteReader();
        while (dr.Read())
            {
            ...
            }
    }
Better still, use Parametrized Queries instead:
public static void SelectMembersAndActuallyReadThemThisTime(string username)
    {
        SqlConnection con = Database.GetConnection();
        SqlCommand cmd = new SqlCommand("select * from members where username=@UN", con);
        cmd.Parameters.AddWithValue("@UN", username);
        SqlDataReader dr=cmd.ExecuteReader();
        while (dr.Read())
            {
            ...
            }
    }



Update from OP:

"if i have declare the sqldatareader in class file then how can i compare variables from another form textfield
like:
if (dr["username"].ToString() == txtUserNamr.Text)

how can i access txtUserName from member.aspx to class file"


In short: Don't.

The longer version: Don't do it. If you start accessing form variables in your class, you ties the two together: you cannot make changes to one without changing the other. This reduces reusability, and makes maintenance harder, as well as potentially reducing reliability.

Why do you need to?

You pass the username through to your routine, don't you? That is what the "username" parameter is for, isn't it?
If it isn't, what is it for?
You have already filtered the results from your database so it only returns the records that have that username: So where is the point in comparing it against a different value?
 
Share this answer
 
v2
Comments
rahul dev123 14-May-11 4:07am    
if i have declare the sqldatareader in class file then how can i compare variables from another form textfield
like:
if (dr["username"].ToString() == txtUserNamr.Text)

how can i access txtUserName from member.aspx to class file
OriginalGriff 14-May-11 4:18am    
Answer updated
 SqlConnection con = Database.GetConnection();
SqlDataAdapter adp = new SqlDataAdapter("select * from members where username=@" + username + "",con);
       DataSet ds = new DataSet();
        adp.Fill(ds);


Hope this can help you.
 
Share this answer
 
mssqlserver in code for select

SQL
create procedure select_employee6(@empid int,
                                  @empcode varchar(20),@empname varchar(20))
as
select * from employee

classcode in c#
C#
public DataSet select()
    {
        Connection c = new Connection();
        //SqlCommand cmd = new SqlCommand("select_employee", c.con);
        SqlDataAdapter da = new SqlDataAdapter("select_employee6", c.con1 );
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        DataSet ds = new DataSet();
        da.Fill(ds);
        return ds;
    }

designpage code in asp.net pageload and one method is created then inside the
C#
private void bind()
    {
        tech t = new tech();
        DataSet ds = t.select();
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

some errors occured in classcode in this line in above sequence

da.fill(ds);
how to get errors occured . otherwise
is it possible u get a idea for me in write the code for my mail anwar.anwa86@gmail.com

how to get the result
[Edit: Added PRE tags, removed empty lines]
 
Share this answer
 
v2

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