开发者

While reading the data from database problem

开发者 https://www.devze.com 2022-12-21 06:28 出处:网络
I have rectified my previous problem and now able to insert data to the table. Now while reading the data with Name, Age, Gender,City, TypeofDisease condition not getting the result..as a result alway

I have rectified my previous problem and now able to insert data to the table.

Now while reading the data with Name, Age, Gender,City, TypeofDisease condition not getting the result..as a result always I'm getting "System.Data.SqlClient.SqlDataReader" Here is my stored procedure:

CREATE PROCEDURE SearchPatient
(
@Name varchar(50),
@Age int,
@Gender varchar(50),
@City varchar(50),
@TypeofDisease varchar(50)
)
 AS
select * from Patient where Name=@Name and Age=@Age and Gender=@Gender and City=@City and TypeofDisease=@TypeofDisease
GO

And here is my event code:

protected void BtnSearch_Click(object sender, EventArgs e)
    { 
        string name = TxtName.Text.Trim();
        int age = Convert.ToInt32(TxtAge.Text);
        string gender;
        if (RadioButtonMale.Checked)
        {
            gender = RadioButtonMale.Text;
        }
        else
        {
            gender = RadioButtonFemale.T开发者_运维百科ext;
        }
        string city = DropDownListCity.SelectedItem.Value;

        string typeofdisease = "";
        foreach (ListItem li in CheckBoxListDisease.Items)
        {
            if (li.Selected)
            {
                typeofdisease += li.Value;
            }
        }
        string[] sl=typeofdisease.Split();
        foreach (string s in sl)
        {
            ListItem itm = CheckBoxListDisease.Items.FindByValue(s);
            if (itm != null)
            {
                itm.Selected = true;
            }
        }
        string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
        SqlConnection con = new SqlConnection(ConString);
        con.Open();
        SqlCommand com = new SqlCommand("SearchPatient", con);
        com.CommandType = CommandType.StoredProcedure;
        try
        {
            com.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = name;
            com.Parameters.Add("@Age", SqlDbType.Int).Value = age;
            com.Parameters.Add("@Gender", SqlDbType.VarChar, 50).Value = gender;
            com.Parameters.Add("@City", SqlDbType.VarChar, 50).Value = city;
            com.Parameters.Add("@TypeofDisease", SqlDbType.VarChar, 50).Value = sl.ToString();
            SqlDataReader dr;
            dr = com.ExecuteReader();
            StringBuilder sb = new StringBuilder();
            sb.Append(dr);
            lblPatientDetails.Text = sb.ToString();          
        }
        catch
        {

            throw;
        }
        finally
        {
            con.Close();
            com.Dispose();
        }
    }

Pls somebody modify my code, to achieve my expected result..


You need to use methods from dr, the SqlDataReader. For example

  • dr.GetValue() (To get a given column from the current row)
  • dr.GetValues() (To get all values from current row)
  • dr.NextResult() (to get to the next row of data in the resultset)

The reason why you keep getting "System.Data.SqlClient.SqlDataReader" instead of some result is that assigning dr to a string (by way of StringBuilder.Append) uses, implicitly, the ToString() method of this object which produces this particular string (a useful debug feature, but otherwise not very applicable one).


 dr = com.ExecuteReader();
 StringBuilder sb = new StringBuilder();
 sb.Append(dr);
 lblPatientDetails.Text = sb.ToString();  

Change to :

using(SqlDataReader rdr = com.ExecuteReader())
{
     if(rdr.HasRows)
     {
       while(rdr.Read())
       {
        sb.Append(rdr.GetString(0)); 
        ..... etc
       }
     }
}

Or You can create DataSet.

    using (SqlDataAdapter dataAdapter = new SqlDataAdapter(com))
    {
        DataSet dataSet = new DataSet();
        dataAdapter.Fill(dataSet);
        return dataSet;
    }
0

精彩评论

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