Click here to Skip to main content
15,885,767 members

Comments by Slacker89 (Top 11 by date)

Slacker89 10-May-13 3:18am View    
Bingo!!! I found the culprit :P I have to load event Monthdata at the end instead of first in ddprojectid_selectedindexchanged event. May be its taking null at first and then passing a value less than current selection to stored procedure. I found it using a label to display projectid selection before adapter fills in Monthdata().
i.e (In ddprojectid_SelectedIndexChanged event)I changed this
finally
{
Monthdata()
conn.Close();
}
Thank you very much for your valuable support. I have learnt something new today. Appreciate it Aarti :)
Slacker89 10-May-13 2:26am View    
That works with a small bug. For Ex: I select a Projectid for which BeginDate = 10/1/2012 and EndDate = 6/1/2013. It is showing the value of previous projectID calculated " MonthName" instead of showing for current selection. Once I select next projectid, previous rows MonthNames are pulled. Why is this happening? Stored Procedure works fine in SQL Server. Here is my code

private void Monthdata()
{
var str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
var conn = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("Addmonthslist", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ProjectID", SqlDbType.VarChar).Value = ddprojectid.SelectedItem.Value;
cmd.Parameters.Add("@BeginDate", SqlDbType.VarChar).Value = tbbegin.Text;
cmd.Parameters.Add("@EndDate", SqlDbType.VarChar).Value = tbending.Text;
var adap = new SqlDataAdapter(cmd);
var ds = new DataSet();
adap.Fill(ds);
ddmonth.DataSource = ds.Tables[1];
ddmonth.DataTextField = "MonthName";
ddmonth.DataValueField = "MonthName";
ddmonth.DataBind();

}
protected void ddprojectid_SelectedIndexChanged(object sender, EventArgs e)
{
Monthdata(); // Populating above MonthData event on selection of ProjectID

var str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string qry = "select CONVERT(VARCHAR(8), BeginDate, 1) AS BeginDate, CONVERT(VARCHAR(8), EndDate, 1) AS EndDate from tblProjects where ProjectID ='" + ddprojectid.SelectedItem.Value + "'";
SqlConnection conn = new SqlConnection(str);

SqlCommand cmd = new SqlCommand(qry, conn);
SqlDataReader sdr;

try
{

if (conn.State != ConnectionState.Open)
conn.Open();

sdr = cmd.ExecuteReader();
while (sdr.Read())
{
tbbegin.Text = sdr["BeginDate"].ToString();
tbending.Text = sdr["EndDate"].ToString();



}


}

catch (Exception ex)
{

lblError.Text = ex.ToString();

}

finally
{


conn.Close();

}

}
Slacker89 10-May-13 1:16am View    
My dumbness, Output of the SQL function wrote above i.e MonthName is not a column of the table. Table has only BeginDate, EndDate and ProjectID. That function takes these input and gives me output in the name of MonthName. How do I store "MonthName" result in a table? Is there any alternative for this problem? I am really sorry for confusing you.
Slacker89 9-May-13 19:13pm View    
I tried as you mentioned but I cannot get Displaymember as a property for dropdown. Here is my code, I used datatext and datavalue property( I know its wrong) since displaymember is not working. Please have a look and correct it. Appreciate your time and patience.
Error I got "DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'MonthName'."
private void Monthdata()
{
var str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
var conn = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("Addmonthslist", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ProjectID", SqlDbType.VarChar).Value = ddprojectid.SelectedItem.Value;
cmd.Parameters.Add("@BeginDate", SqlDbType.VarChar).Value = ddbegin.Text;
cmd.Parameters.Add("@EndDate", SqlDbType.VarChar).Value = ddend.Text;
var adap = new SqlDataAdapter(cmd);
DataTable myDataTable = new DataTable();
adap.Fill(myDataTable);
ddmonth.DataSource = myDataTable;
ddmonth.DataTextField = "MonthName";
ddmonth.DataValueField = "MonthName";
ddmonth.DataBind();
}
Slacker89 9-May-13 3:38am View    
Thanks for your quick solution. It works like charm . You are awesome. How should I display this result in a dropdown. I tried using SQL dataadapter but it gave me an error since I am not declaring the output result anywhere. Please help me :(