can anyone tell me how to use if else statement using c# and MS Access database..this is my code..

protected void Button2_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath("App_Data\\dnis2010.mdb");
        OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path);
        //con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\keOn_kAyFar\\Documents\\Visual Studio 2008\\WebSites\\DNIS\\App_Data\\dnis2010.mdb;Persist Security Info=False";

        conn.Open();
        string str = "SELECT Type,Department,COUNT(RAM) FROM tableRegister WHERE RAM LIKE '%" + TextBox1.Text + "%' GROUP BY Type,Department";

        OleDbDataAdapter da = new OleDbDataAdapter(str, conn);
        DataSet ds = new DataSet();
        ds.Clear();
        da.Fill(ds, "tableRegister");
        GridView1.DataSource = ds;
        GridView1.DataMember = "tableRegister";
        GridView1.DataBind();

        conn.Close();
    }

Recommended Answers

All 14 Replies

Can you please explain in detail where and why you want to use? anothething it's also very simple to define If..Else .

can anyone tell me how to use if else statement using c# and MS Access database..this is my code..

protected void Button2_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath("App_Data\\dnis2010.mdb");
        OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path);
        //con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\keOn_kAyFar\\Documents\\Visual Studio 2008\\WebSites\\DNIS\\App_Data\\dnis2010.mdb;Persist Security Info=False";

        conn.Open();
        string str = "SELECT Type,Department,COUNT(RAM) FROM tableRegister WHERE RAM LIKE '%" + TextBox1.Text + "%' GROUP BY Type,Department";

        OleDbDataAdapter da = new OleDbDataAdapter(str, conn);
        DataSet ds = new DataSet();
        ds.Clear();
        da.Fill(ds, "tableRegister");
        GridView1.DataSource = ds;
        GridView1.DataMember = "tableRegister";
        GridView1.DataBind();

        conn.Close();
    }

i want to use this when i want to count in various..let say if i want to count other than RAM only..how can i use if else statement?
can u understand what i'm trying to tell here?

something like this..

if(what i need to write here)
select Type,Department, COUNT (RAM) WHERE RAM LIKE '%"+ TextBox1.Text +"%' GROUP BY Type,Department;

else(what i need to write here)
SELECT Type,Department,COUNT(Windows) WHERE Windows LIKE '%"+ TextBox1.Text +"%' GROUP BY Type,Department;


and then what should i write then??

correct me if i'm wrong..thanks

do you have dropdown or something like that to select RAM, Windows type of option ? If yes then you create query dynamically and fetch the record. no need for If..Else condition. See the below query.

strSQL = "Select Type, Department, COUNT(" + dropdown1.SelectedItem.Text + ") From yourTablename WHERE " + dropdown1.SelectedItem.Text + " Like '%" + TextBox1.Text +"%' GROUP BY Type,Department;
SqlDataAdapter adpt = new SqlDataAdapter(strSQL, yourconnection);
DataTable dt = new DataTable();
adpt.Fill(dt);

So by this way you don't need to check for any condition and i believe this is the proper way. Otherwise i think it's not possible to check for RAM or Windows without any inputs.

Hope it will help you and please Let us know if it's still unclear to you.

something like this..

if(what i need to write here)
select Type,Department, COUNT (RAM) WHERE RAM LIKE '%"+ TextBox1.Text +"%' GROUP BY Type,Department;

else(what i need to write here)
SELECT Type,Department,COUNT(Windows) WHERE Windows LIKE '%"+ TextBox1.Text +"%' GROUP BY Type,Department;


and then what should i write then??

correct me if i'm wrong..thanks

i'm using MS Access database..can you convert it into MS Access?

i didn't have a dropdown yet..so i need to add dropdown 1st in order to use this?


look at my attach..
how i want to change the header from exp1002 into total?
did you know how to change it?

regarding your this question, " i didn't have a dropdown yet..so i need to add dropdown 1st in order to use this?". I just say it's depend on your requirement and functioning. Anotherthing i just want to know, does RAM, Windows etc are columns of your table ? If yes then all those columns in one table or in different table ?

regarding your below, you can use HeaderTemplate or give alias name in your select query.

[ATTACH]16231[/ATTACH]

look at my attach..
how i want to change the header from exp1002 into total?
did you know how to change it?

what do you mean by giving alias name?where should i put alias on?

actually i already put DDL..and it working well..it just about the header of the total..it appear expr1002 as i attach above..what can i do to make it name as total..

I believe you are showing Type, Department, Expr1002 data in GridView. If so then there are two ways :

1) In SELECT Query write like "SELECT Type, Department, Expr1002 as Total from your table name"
2) in the HeaderTemplate of your gridview for Expr1002 column, set Total in HeaderText property.

Both the ways work for you :-)

1)where should i put select query?
2)i didn't have header template of my grid view..

so,what should i do? i didn't set my gridview header..it appear like that..

As you are saying that you have already defined DDL and it's working fine. What i was saying in your existing DDL statement you just need to write "expr1002 as Total" nothing else.

Another thing, I guess you have defined columns in below way in GridView. So you just need to set HeaderText property :

<Columns>
            <asp:BoundField DataField="expr1002" HeaderText="Total" />
        </Columns>

1)where should i put select query?
2)i didn't have header template of my grid view..

so,what should i do? i didn't set my gridview header..it appear like that..

Hi..

If..Else its a basic thing in asp.net.I will provide the code for if..else.I think it will help you to understand how to use if..else loop.

Example::

If TopicTxt.Text = "" Then

lblOutCome.Text = "Blah"

Else

lblOutCome.Text = "FooBar"

EndIf

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.