0

Hi..
now i'm working with asp.net c#.. i want to view the data from database in the textbox in order to make editing to the data is avaiable..but i'm using user stored procedure..

now this is my code where i want to display the data..

<tr>    
        <td class="style1">Company Code </td>
        <td>:</td>
        <td>
            <asp:TextBox ID="txtCmpCode" runat="server" Width="80" BorderStyle="Groove" MaxLength="10" ReadOnly="true"></asp:TextBox   
            
            Company Name: <asp:TextBox ID="txtCmpName" runat="server" Width="217" BorderStyle="Groove">      
        </td>
    </tr>

for the back end code is

protected void Page_Load(object sender, EventArgs e)
    {
        string strCompCode = Request.QueryString["comp_code"].ToString();
        SearchData(strCompCode);
    }
    //SearchData
    public void SearchData(string CmpCode)
    {
        //Define Class gdex
        gdexClass gd = new gdexClass();

        //Open Database Connection 
        SqlConnection sqlconnection = gd.GetConnection();

        //Define Store Procedure
        SqlCommand Cmd = new SqlCommand("usp_TbCompany_sc", sqlconnection);
        Cmd.CommandType = CommandType.StoredProcedure;

        //Bind Data from tbe control to Stored Procudure 
        Cmd.Parameters.AddWithValue("@CmpCode", CmpCode);

        Cmd.Parameters.Add("@CmpCode", SqlDbType.NVarChar, 10);
        Cmd.Parameters.Add("@CmpName", SqlDbType.NVarChar, 100);

        String Comp_Code = (string)Cmd.Parameters["@CmpCode"].Value;
        String Comp_Name = (string)Cmd.Parameters["@CmpName"].Value;

        Cmd.Connection = sqlconnection;
        sqlconnection.Open();

        txtCmpCode.Text = Comp_Code;
        txtCmpName.Text = Comp_Name;
        sqlconnection.Close();

for the txtCmpCode is successfully display because of the parameter passing, but for the rest i dont know what should i do else..i had tried many ways but not solve yet..
please anybody can help me..

Edited by new SE: n/a

2
Contributors
8
Replies
23
Views
7 Years
Discussion Span
Last Post by new SE
0

txtCompCode is successfully displayed because you are showing the value which you are passing in your method SearchData(CmpCode). You are not really display Company Code to your textbox from database.

I assume you want to display Company Name based on Company Code that you are passing to your Stored Procedure. You have written some wrong code. correct code may looks like below. Try below code :

/* no need for this four lines. So just comment it
Cmd.Parameters.Add("@CmpCode", SqlDbType.NVarChar, 10);        
Cmd.Parameters.Add("@CmpName", SqlDbType.NVarChar, 100);         
String Comp_Code = (string)Cmd.Parameters["@CmpCode"].Value;        
String Comp_Name = (string)Cmd.Parameters["@CmpName"].Value;
*/

//Define one SqlDataReader in your method say
SqlDataReader objDR;
objDR = Cmd.ExecuteReader(); // here your procedure gets execute
if(objDR.Read())
{
txtCmpName.Text = objDR["your companyname column name from database"].ToString();
}

objDR.Close(); // alwayz once your operation complete close datareader.

that;s all.. try by this and let us know

0

thank you very much..
i also want to apply this code to dropdownlist but it not display any data.. i have make some changes to selected.value but also cannot make it..

txtCmpCode.Text = objDR["CmpCode"].ToString();
txtCmpName.Text = objDR["CmpName"].ToString();
txtCmpAddr1.Text = objDR["CmpAddr1"].ToString();
txtCmpAddr2.Text = objDR["CmpAddr2"].ToString();
txtCmpCityCode.Text = objDR["CmpCityCode"].ToString();
txtCmpCityName.Text = objDR["CmpCityName"].ToString();
txtCmpZip.Text = objDR["CmpZip"].ToString();
ddlCmpStateCode.SelectedValue = objDR["CmpStateCode"].ToString();
0

For dropdown you need to write code like below...

ddlCmpStateCode.SelectedValue = ((ListItem)DropDownList1.Items.FindByValue(objDR["CmpStateCode"].ToString())).Value;
((ListItem)ddlCmpStateCode.Items.FindByValue(objDR["CmpStateCode"].ToString())).Selected = true;

The reason for above code is that the way you had written code for dropdown is that you are just assigning the value from database to SelectedValue of dropdown. You are not really finding it in your Item collection of dropdown which already binded with database. That's why it was not showing.

that's all.. try above code and let us know...

thank you very much..
i also want to apply this code to dropdownlist but it not display any data.. i have make some changes to selected.value but also cannot make it..

txtCmpCode.Text = objDR["CmpCode"].ToString();
txtCmpName.Text = objDR["CmpName"].ToString();
txtCmpAddr1.Text = objDR["CmpAddr1"].ToString();
txtCmpAddr2.Text = objDR["CmpAddr2"].ToString();
txtCmpCityCode.Text = objDR["CmpCityCode"].ToString();
txtCmpCityName.Text = objDR["CmpCityName"].ToString();
txtCmpZip.Text = objDR["CmpZip"].ToString();
ddlCmpStateCode.SelectedValue = objDR["CmpStateCode"].ToString();
0

Thanks again..i have tried this code but error apperar

Object reference not set to an instance of an object.

May be this is because of the record from ddlCmpStateCode is taken form other table..am i right?

this is my stored procedure

ALTER PROCEDURE [dbo].[usp_TbCompany_sd] 

@CmpID nvarchar(10)

AS
BEGIN

	SET NOCOUNT ON;

	SELECT *
	from Tb_Company 
	WHERE CmpID=@CmpID
END

should i select from other table too? which is table state that store the record state..
but there is no CmpID in table state

Edited by new SE: n/a

0

Show me your code of dropdown..I believe it shouldn't have to give you this error...
Also try by below way by checking NULL condition as you are saying that record taken from other table..

ListItem li = ddlCmpStateCode.Items.FindByValue(objDR["CmpStateCode"].ToString());
if (li != null)
{
    ddlCmpStateCode.SelectedValue = li.Value;
    li.Selected = true;
}

try by this and let us know and please don;t forget to put your code if error still occurs...

Thanks again..i have tried this code but error apperar

Object reference not set to an instance of an object.

May be this is because of the record from ddlCmpStateCode is taken form other table..am i right?

this is my stored procedure

ALTER PROCEDURE [dbo].[usp_TbCompany_sd] 

@CmpID nvarchar(10)

AS
BEGIN

	SET NOCOUNT ON;

	SELECT *
	from Tb_Company 
	WHERE CmpID=@CmpID
END

should i select from other table too? which is table state that store the record state..
but there is no CmpID in table state

Edited by rohand: n/a

0

This is the code i'm using now that given the error..

public void SearchData(string CmpID)
    {
        //Define Class gdex
        gdexClass gd = new gdexClass();

        //Open Database Connection 
        SqlConnection sqlconnection = gd.GetConnection();
        sqlconnection.Open();

        //Define Store Procedure
        SqlCommand Cmd = new SqlCommand("usp_TbCompany_sd", sqlconnection);
        Cmd.CommandType = CommandType.StoredProcedure;

        //Bind Data from tbe control to Stored Procudure 
        Cmd.Parameters.AddWithValue("@CmpID", CmpID);
        //Cmd.Parameters.AddWithValue("@StatCode", ddlCmpStateCode.SelectedValue);

        //Define one SqlDataReader 
        SqlDataReader objDR;
        objDR = Cmd.ExecuteReader(); // here procedure gets execute
        if (objDR.Read())
        {
            
            txtCmpStateCode.Text = objDR["CmpStateName"].ToString();

            ddlCmpStateCode.SelectedValue = ((ListItem)ddlCmpStateCode.Items.FindByValue(objDR["CmpStateCode"].ToString())).Value;
            ((ListItem)ddlCmpStateCode.Items.FindByValue(objDR["CmpStateCode"].ToString())).Selected = true;

            ListItem li = ddlCmpStateCode.Items.FindByValue(objDR["CmpStateCode"].ToString());
            if (li != null)
            {
                ddlCmpStateCode.SelectedValue = li.Value;
                li.Selected = true;
            }
                     
            txtCmpEffDate.Text = objDR["CmpEffDate"].ToString().Substring(0, 10);
            txtCmpExpDate.Text = objDR["CmpExpDate"].ToString().Substring(0, 10);
            txtCmpDBlink.Text = objDR["CmpDBlink"].ToString();
        }
        // complete close datareader.
        objDR.Close(); 

    }

usp_TbCompany_sd = contain all information about the company but does not contain record of state
Error = NullReferenceException: Object reference not set to an instance of an object
i have added the latest code, it doesnt read it and still give the same error

Edited by new SE: n/a

0

hey if your stored procedure is not containing any information about States then obviously it was throwing error.

Another thing you don;t need to write line of both the codes i have sent to you either first one or second one...

//either this 
ddlCmpStateCode.SelectedValue = ((ListItem)ddlCmpStateCode.Items.FindByValue(objDR["CmpStateCode"].ToString())).Value;
((ListItem)ddlCmpStateCode.Items.FindByValue(objDR["CmpStateCode"].ToString())).Selected = true;

// or this
ListItem li = ddlCmpStateCode.Items.FindByValue(objDR["CmpStateCode"].ToString());
if (li != null)
{
    ddlCmpStateCode.SelectedValue = li.Value;
    li.Selected = true;
}

Also Can you please let me know are you showing State name in Dropdown or in textbox (txtCmpStateCode.Text).

This is the code i'm using now that given the error..

public void SearchData(string CmpID)
    {
        //Define Class gdex
        gdexClass gd = new gdexClass();

        //Open Database Connection 
        SqlConnection sqlconnection = gd.GetConnection();
        sqlconnection.Open();

        //Define Store Procedure
        SqlCommand Cmd = new SqlCommand("usp_TbCompany_sd", sqlconnection);
        Cmd.CommandType = CommandType.StoredProcedure;

        //Bind Data from tbe control to Stored Procudure 
        Cmd.Parameters.AddWithValue("@CmpID", CmpID);
        //Cmd.Parameters.AddWithValue("@StatCode", ddlCmpStateCode.SelectedValue);

        //Define one SqlDataReader 
        SqlDataReader objDR;
        objDR = Cmd.ExecuteReader(); // here procedure gets execute
        if (objDR.Read())
        {
            
            txtCmpStateCode.Text = objDR["CmpStateName"].ToString();

            ddlCmpStateCode.SelectedValue = ((ListItem)ddlCmpStateCode.Items.FindByValue(objDR["CmpStateCode"].ToString())).Value;
            ((ListItem)ddlCmpStateCode.Items.FindByValue(objDR["CmpStateCode"].ToString())).Selected = true;

            ListItem li = ddlCmpStateCode.Items.FindByValue(objDR["CmpStateCode"].ToString());
            if (li != null)
            {
                ddlCmpStateCode.SelectedValue = li.Value;
                li.Selected = true;
            }
                     
            txtCmpEffDate.Text = objDR["CmpEffDate"].ToString().Substring(0, 10);
            txtCmpExpDate.Text = objDR["CmpExpDate"].ToString().Substring(0, 10);
            txtCmpDBlink.Text = objDR["CmpDBlink"].ToString();
        }
        // complete close datareader.
        objDR.Close(); 

    }

usp_TbCompany_sd = contain all information about the company but does not contain record of state
Error = NullReferenceException: Object reference not set to an instance of an object
i have added the latest code, it doesnt read it and still give the same error

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.