如果数据库中没有数据,则Pageload应显示默认值 [英] Pageload should display default values if no data in database

查看:56
本文介绍了如果数据库中没有数据,则Pageload应显示默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我的网络表格中有一个文本框,两个下拉列表和一个按钮。



1.在页面加载时,如果选择了任何用户,那么该用户的这些文本框,下拉列表值应该从数据库中显示。我已经完成了这个并且工作得非常好。



2.如果数据库中没有用户,则文本框,下拉列表应显示默认值。





请任何人都可以帮我这样做。我尝试了2位但没有成功。

当我尝试在数据库中没有值时抛出错误。





谢谢





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


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();
        }






















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();
        }







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
     
    )

推荐答案

只需更换你的代码:

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



with


with

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";
}


使用以下条件检查您的读卡器是否有值: -



Check your reader that it has values or not using this condition:-

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());
}


hi,



在查询中使用isnull()函数。 ..







Use isnull() function in your query...


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

 where department='Account'





检索数据后,检查datareader或数据集是否有数据......

然后根据检索到的值,您可以设置文本框值..



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..


这篇关于如果数据库中没有数据,则Pageload应显示默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆