2013-05-30 3 views
0

나는 2 개의 필드 속성, 값 및 3 개의 텍스트 상자가있는 gridview 있습니다. 값을 입력 할 때 일부 텍스트 상자를 비워 두었습니다. 빈 textboxes를 확인해야합니다. 빈 값은 데이터베이스에 삽입하고 싶지 않습니다. 이 문제를 어떻게 해결할 수 있습니까 ???asp.net에서 텍스트 상자의 비어 있음을 확인하는 방법은 무엇입니까?

이것은

foreach (GridViewRow gvr in GridView1.Rows) 
{ 
    string strcon1; 
    strcon1 = ConfigurationManager.ConnectionStrings["fwma_devConnectionString"].ConnectionString; 

    SqlConnection con1 = new SqlConnection(strcon1); 
    con1.Open(); 

    SqlCommand com3 = new SqlCommand(strcon); 

    TextBox tb = (TextBox)gvr.FindControl("TextBox2");//value 

    string txt = tb.Text; 

    Label propertylabel = (Label)gvr.FindControl("Label4");//id-property 

    com3.CommandText = "INSERT INTO BrandProperties(PropertyID,BrandID,[Values]) values('" + propertylabel.Text + "','" + B_id.Text + "','" + tb.Text + "')"; 
    com3.Connection = con1; 
    com3.ExecuteNonQuery(); 

    con1.Close(); 

    if (tb.Text == String.Empty) 
    { 

    } 
} 
+0

이 비어있을 때 몇 가지 경고를 표시해야합니까? 삽입을 중단 하시겠습니까? – Fals

+0

그냥 빈 텍스트 상자 삽입을 중단하고 싶습니다. 경고를 표시하고 싶지 않습니다. –

+0

'insert' 쿼리가 아니라 'insert'쿼리 전에'null' 또는'Empty'를 확인하십시오. – Rahul

답변

1

코드를 재구성하여 먼저 텍스트 상자 컨트롤을 가져온 다음 비어 있지 않은 문자열을 확인해야합니다. 비어 있지 않은 경우 DB 삽입을 수행하십시오.

foreach (GridViewRow gvr in GridView1.Rows) 
{ 
    TextBox tb = (TextBox)gvr.FindControl("TextBox2");//value 

    if (!string.IsNullOrEmpty(tb.Text)) 
    { 
    string strcon1; 
    strcon1 = ConfigurationManager.ConnectionStrings["fwma_devConnectionString"].ConnectionString; 

    SqlConnection con1 = new SqlConnection(strcon1); 
    con1.Open(); 

    SqlCommand com3 = new SqlCommand(strcon); 

    Label propertylabel = (Label)gvr.FindControl("Label4");//id-property 

    com3.CommandText = "INSERT INTO BrandProperties(PropertyID,BrandID,[Values]) values('" + propertylabel.Text + "','" + B_id.Text + "','" + tb.Text + "')"; 
    com3.Connection = con1; 
    com3.ExecuteNonQuery(); 

    con1.Close(); 
    } 
} 
1

이 확인되어 내 코드?

// 코드

TextBox tb = (TextBox)gvr.FindControl("TextBox2");//value 
string txt = tb.Text; 
If(tb.Text!=string.IsNullOrEmpty) 
{ 
    //Do your stuff 
} 
+0

팁 하나만! 이런 종류의 비교를 위해 string.IsNullOrEmpty를 사용하십시오! – Fals

+0

물론 가능합니다. – iamCR

+0

동일한 빈 행을 데이터베이스에 삽입합니다. –

1

당신은

If(tb.Text!= string.Empty) 
{ 
    //Do your stuff 
} 

또는

If(tb.Text!="") 
{ 
    //Do your stuff 
} 

위해 시도하거나, insert 쿼리하기 전에 null 또는 Empty를 확인하기 위해 시도 할 수 있습니다.

if (!string.IsNullOrEmpty("tb.Text")) 
{ 
    // Do Your stuff 
} 

희망 사항.

+0

nop .. 데이터베이스에 같은 빈 행을 삽입. –

+1

마지막 로직'! string.IsNullOrEmpty ("tb.Text")'.. – Rahul

+0

... ..(데이터베이스 행에 공백 텍스트 상자 삽입하기 –

1

최상의 아이디어는 해당 입력란에 필수 필드 유효성 검사기를 사용하는 것입니다. 그런 다음

string.IsNullOrEmpty (tb.Text) 같은 이중 검사를 추가 할 수 있습니다

관련 문제