Click here to Skip to main content
15,906,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have a textbox, 2-dropdownlists and a button in my webform.

1.On Page load , if any user is selected then these textbox,dropdownlist values for that user should display from the database .I have done this and is working perfectly fine.

2. If there is no user in the database then the textbox,dropdownlist should display default values.


Please can anyone help me in doing this. I tried the 2 bit but no success.
When i tried with no values in the database its throwing error.


Thanks


C#
protected void Page_Load(object sender, EventArgs e)
        {
            
            Loaddetails();
            
        }


private void Loaddetails()
        {
            string connectionString2 = ConfigurationManager.AppSettings["Services"];
            SqlConnection myconnection2 = new SqlConnection(connectionString2);
            SqlCommand cmd2 = new SqlCommand("select TAnalysis,vendor,Biographies from Auth where Authority=@Authority");
            SqlDataReader reader2;
            cmd2.Parameters.AddWithValue("@Authority", Session["Sessionuser"]);
            cmd2.Connection = myconnection2;
            myconnection2.Open();
            reader2 = cmd2.ExecuteReader();
            reader2.Read();
            trendtxtbox.Text = (reader2["Analysis"].ToString());
            DropDownList1.Text = (reader2["vendor"].ToString());
            splitbioddl.Text = (reader2["Biographies"].ToString());
            reader2.Close();
            myconnection2.Close();
        
        }


// to insert the values on button click


 protected void saveauthbtn_Click(object sender, EventArgs e)
        {
            string connectionString = ConfigurationManager.AppSettings["Services"];
            SqlConnection myconnection = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand("[sp_InsertandupdateAuthority]", myconnection);
            cmd.Parameters.AddWithValue("@Authority", Session["Sessionuser"]);
            cmd.Parameters.AddWithValue("@AuthorityName", authoritylabel.Text);
            cmd.Parameters.AddWithValue("@vendor", DropDownList1.SelectedItem.Value);
            cmd.Parameters.AddWithValue("@Biographies", splitbioddl.SelectedItem.Value);
            cmd.Parameters.AddWithValue("@Analysis", trendtxtbox.Text);
            DropDownList1.Items.Remove(DropDownList1.SelectedItem.Value);
            splitbioddl.Items.Remove(splitbioddl.SelectedItem.Value);
            trendtxtbox.Text = "";
            cmd.CommandType = CommandType.StoredProcedure;
            myconnection.Open();
            cmd.Connection = myconnection;
            cmd.ExecuteNonQuery();
            myconnection.Close();
        }







C#
protected void Page_Load(object sender, EventArgs e)
        {
            
            Loaddetails();
            
        }


 private void Loaddetails()
        {

                string connectionString2 = ConfigurationManager.AppSettings["Services"];
                SqlConnection myconnection2 = new SqlConnection(connectionString2);
                SqlCommand cmd2 = new SqlCommand("select Analysis,vendor,Biographies from Admin_Authority where Authority=@Authority");
                SqlDataReader reader2;
                cmd2.Parameters.AddWithValue("@Authority", Session["Sessionuser"]);
                cmd2.Connection = myconnection2;
                myconnection2.Open();
                reader2 = cmd2.ExecuteReader();      

                if (reader2.HasRows)//Values found in database
                {
                    reader2.Read();
                    trendtxtbox.Text = (reader2["Analysis"].ToString());
                    DropDownList1.Text = (reader2["vendor"].ToString());
                    splitbioddl.Text = (reader2["Biographies"].ToString());        

                }
                else//Populate the controls with default values
                {
                    trendtxtbox.Text = "5";
                    DropDownList1.Text = "M";
                    splitbioddl.Text = "No";
                }

                reader2.Close();
                myconnection2.Close();

            }




// to insert the values on button click


 protected void saveauthbtn_Click(object sender, EventArgs e)
        {
            string connectionString = ConfigurationManager.AppSettings["Services"];
            SqlConnection myconnection = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand("[sp_InsertAdmin_Authority]", myconnection);
            cmd.Parameters.AddWithValue("@Authority", Session["Sessionuser"]);
            cmd.Parameters.AddWithValue("@AuthorityName", authoritylabel.Text);
            cmd.Parameters.AddWithValue("@vendor", DropDownList1.SelectedItem.Value);
            cmd.Parameters.AddWithValue("@Biographies", splitbioddl.SelectedItem.Value);
            cmd.Parameters.AddWithValue("@Analysis", trendtxtbox.Text);
            DropDownList1.Items.Remove(DropDownList1.SelectedItem.Value);
            splitbioddl.Items.Remove(splitbioddl.SelectedItem.Value);
            trendtxtbox.Text = "";
            cmd.CommandType = CommandType.StoredProcedure;
            myconnection.Open();
            cmd.Connection = myconnection;
            cmd.ExecuteNonQuery();
            myconnection.Close();
        }


SQL
stored procedure



ALTER PROCEDURE [dbo].[sp_InsertAdmin_Authority]
    (
      
    @Authority int ,
    @AuthorityName nvarchar(50),
    @vendor nvarchar(50),
    @Biographies nvarchar(3),
    @Analysis nvarchar(10)
    
    )
    AS

    INSERT INTO Admin_Authority
    (
      Authority,
      AuthorityName,
      vendor,
      Biographies,
      Analysis
    )
    VALUES
    (
   @Authority,
   @AuthorityName,
   @vendor,
   @Biographies,
   @Analysis
     
    )
Posted
Updated 18-Apr-13 0:31am
v5
Comments
Zafar Sultan 18-Apr-13 4:25am    
Post your code here. That will be more useful for us to figure out the error you are getting.
babli3 18-Apr-13 5:14am    
I have posted my code. Thanks
Karthik Harve 18-Apr-13 4:36am    
where you stuck ? where is the difficulty ?
babli3 18-Apr-13 5:16am    
If there is no data in the database then these web forms should display some default values and let user insert new values.

i am not able to do this....
[no name] 18-Apr-13 4:41am    
hi,
please post your code, that ll be help full

Just replace your code:
C#
reader2 = cmd2.ExecuteReader();
            reader2.Read();
            trendtxtbox.Text = (reader2["Analysis"].ToString());
            DropDownList1.Text = (reader2["vendor"].ToString());
            splitbioddl.Text = (reader2["Biographies"].ToString());

with
C#
reader2 = cmd2.ExecuteReader();
if(reader2.HasRows)//Values found in database
{
            reader2.Read();
            trendtxtbox.Text = (reader2["Analysis"].ToString());
            DropDownList1.Text = (reader2["vendor"].ToString());
            splitbioddl.Text = (reader2["Biographies"].ToString());
}
else//Populate the controls with default values
{
trendtxtbox.Text = "Your default value";
            DropDownList1.Text = "Your default value";
            splitbioddl.Text = "Your default value";
}
 
Share this answer
 
v2
Comments
babli3 18-Apr-13 6:00am    
Hi Zafar

Thanks the code you gave me is working for default if there is nothing in database.
But when i am trying to insert new values , its inserting the default values in the databse.

Thanks
Zafar Sultan 18-Apr-13 6:11am    
The above code is for populating the controls from database. If there is no row in the database the control will be populated with the default values. Now its up to you what default values you are using to populate the controls. I just provided an example. That means else block depends totally upon your functionality. If you can elaborate further, I may be able to provide more help. And if it works for you, you can mark it as answer.
babli3 18-Apr-13 6:16am    
Hi Zafar
Thanks
This code is working fine . But when I am trying to enter any values in text box or dropdown
list, its not taking those values and instead inserting the default values in the database.
Zafar Sultan 18-Apr-13 6:19am    
Means? if there are no values the controls are populated with "default". But when you type or change the values of textboxes, "default" is inserted into the database? Is this the case?
babli3 18-Apr-13 6:22am    
yes
Check your reader that it has values or not using this condition:-

C#
reader2 = cmd2.ExecuteReader();

if (dr == null || !dr.HasRows)
{
     //Default values
     trendtxtbox.Text = "Val1";
     DropDownList1.Text = "Val2";
     splitbioddl.Text = "Val3";
}
else
{
     //DB Values
     trendtxtbox.Text = (reader2["Analysis"].ToString());
     DropDownList1.Text = (reader2["vendor"].ToString());
     splitbioddl.Text = (reader2["Biographies"].ToString());
}
 
Share this answer
 
Comments
babli3 18-Apr-13 9:09am    
Hi Tushar
Thanks

The above code displays the default data on page load but , it is inserting only the default values into the database. Its not inserting the new values i m selecting.Thanks
TrushnaK 18-Apr-13 9:44am    
Put this code in your Loaddetails() method. don't put it in your saveauthbtn_Click(object sender, EventArgs e)
babli3 18-Apr-13 9:51am    
yes I have this in my LoadDetailsmethod(). But still its inserting the default values only.
TrushnaK 19-Apr-13 3:44am    
debug your saveauthbtn_Click() Method what you get in dropdown.selected items and textboxes.Text.
I think this may get default values and you not changed that.
hi,

Use isnull() function in your query...


SQL
select  isnull(username,"") as uname,isnull(lastname,"") from employee

 where department='Account'


After retrieving the data check the datareader or dataset whether it has data or not...
Then based on the value retrieved you can set the textbox value..
 
Share this answer
 
v2
Comments
babli3 18-Apr-13 5:16am    
I tried using is null but isnt working
if No records found try this

if(count>0)
{
//here retriveing code
}
else
{
textbox1.text="Default";
ddl.ClearSelection();
ddl.datasource=null;
ddl.databind();
ddl.items.insert(0,"DefaultValue");
}
 
Share this answer
 
Comments
babli3 18-Apr-13 5:24am    
It says "The name count does not exists in the current context"
babli3 18-Apr-13 5:35am    
Hi
With this code its showing the default values on pageload even if i have data in the database.

I want to show the default values on pageload only if there is no data in the databse ortherwise it shoud display values from databse.

Please help. Thank you
private void Loaddetails()
        {
            string connectionString2 = ConfigurationManager.AppSettings["Services"];
            SqlConnection myconnection2 = new SqlConnection(connectionString2);
            SqlCommand cmd2 = new SqlCommand("select TAnalysis,vendor,Biographies from Auth where Authority=@Authority");
            SqlDataReader reader2;
            cmd2.Parameters.AddWithValue("@Authority", Session["Sessionuser"]);
            cmd2.Connection = myconnection2;
            myconnection2.Open();
            reader2 = cmd2.ExecuteReader();
            reader2.Read();
            if(reader2["Analysis"]!=null)
            {
            trendtxtbox.Text = (reader2["Analysis"].ToString());
            }
            else
            {
                   trendtxtbox.Text = "N/A" ;
            }

            if(reader2["vendor"]!=null)
            {
                DropDownList1.Text = (reader2["vendor"].ToString());
            }
            else
            {
                   DropDownList1.Text =  "N/A" ;
            }

             
            splitbioddl.Text = (reader2["Biographies"].ToString());
            reader2.Close();
            myconnection2.Close();
        
        }
 
Share this answer
 
Comments
babli3 18-Apr-13 5:50am    
HI ,
I tried this code but
I am again getting this error "Invalid attempt to read when no data is present"

Thanks

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