开发者

Problem in using multiple sql queries on single button click

I am using the following code and it works fine.

protected void Button2_Click(object sender, EventArgs e)
    {
        String a = DropDownList1.SelectedItem.Value;
        String b = DropDownList3.SelectedItem.Value.PadLeft(3, '0');      
        String c = TextBox2.Text.PadLeft(5,'0').ToString();
        String d = TextBox3.Text.ToString();
        String digit = a+ b  + c + d;
        try
        {
         myConn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=testcase;User=root;Password=root;Option=3;");
         myConn.Open();
            //**
            string sql = "select * from testcase.main where reg_no =?";            
            //**
            OdbcCommand cmd = new OdbcCommand(sql, myConn);            
            //**
            cmd.Parameters.AddWithValue("?", digit);
            MyReader = cmd.ExecuteReader();
            //**
            while (MyReader.Read())
            {
                String f = MyReader["pet_name"].ToString();
                String g = MyReader["res_name"].ToString();

                Label9.Visible = true;
                Label9.Text = f;

                Label10.Visible = true;
                Label10.Text = "VS";

                //Label11.Visible = true;
                Label11.Text = g;

            }

            MyReader.Clo开发者_JAVA技巧se();
        }
        catch (Exception e1)
        {
            Response.Write(e1.ToString());
        }
        finally
        {
            if (MyReader != null && !MyReader.IsClosed)
            {
                MyReader.Close();   
            }

            if (myConn != null && myConn.State == ConnectionState.Open)
            {
                myConn.Close();
            }
        }

I want to add another sql query which is using two tables to fetch the data and than i want to display it in a dropdownlist after fetching data from database.

How should i proceed?? Should i create a totally new connection? I tried many different ways like, creating new connection and new reader and entire try and catch block i coded again but on running the website i t was taking very long to load the contents.

What i did i modified only this portion(below code) and used the entire try catch again but it dint work.

while (MyReader1.Read())
            {
                String f = MyReader1["ret"].ToString();

             DropDownList1.Items.Add(f);

            }

Please help.


Yes you should create a new connection and a new reader. It's good practice to open and close the connection as soon as possible. If the content was taking awhile to load up, it's most likely not the connection creation. Look at the indexing of the sql tables or how you're loading the content.

From the looks of it, if your query returns multiple results (which I'm guessing it's not?), you will be overwriting same label over and over agin.

You should wrap your OdbcConnection object & MyReader object in using statements. That way you don't need the .close statements & Finally block. It's a bit cleaner.

      private void DoSomething()
    {
        String a = DropDownList1.SelectedItem.Value;
        String b = DropDownList3.SelectedItem.Value.PadLeft(3, '0');
        String c = TextBox2.Text.PadLeft(5, '0').ToString();
        String d = TextBox3.Text.ToString();
        String digit = a + b + c + d;
        String sql = "select * from testcase.main where reg_no =?";

        try
        {
            using (OdbcConnection myConn = new OdbcConnection("Driver={MySQL ODBC 3.51   Driver};Server=localhost;Database=testcase;User=root;Password=root;Option=3;"))
 using(OdbcCommand cmd = new OdbcCommand(sql, myConn))
                {


   myConn.Open();
                    //**
                    cmd.Parameters.AddWithValue("?", digit);
                    using (odbcReader MyReader = cmd.ExecuteReader())
                    {
                        //**
                        while (MyReader.Read())
                        {
                            String f = MyReader["pet_name"].ToString();
                            String g = MyReader["res_name"].ToString();

                            Label9.Visible = true;
                            Label9.Text = f;

                            Label10.Visible = true;
                            Label10.Text = "VS";

                            //Label11.Visible = true;
                            Label11.Text = g;

                        }
                    }
                }
            }
        }
        catch (Exception e1)
        {
            Response.Write(e1.ToString());
        }
    }

}

Edit - Fixed the nested statement


If it is taking long time to load, then check that how many items are being added to the dropdown. Also, you can fetch the contents of the table into a DataTable and then use a foreach loop to add the items to drop down list. Look here for steps to get the data into a datatable. Then, use the below loop:

String f;
foreach(DataRow dr in myTable.Rows)
{
    f = dr["ret"].ToString();
    DropDownList1.Items.Add(f);

}

This should speed up the process.

Thanks,
Vamyip

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜